jbrb
January 17, 2020, 3:41pm
1
Hi, I have a query for payments that I need to filter before summing up the values. I only need the paid ones.
type(sum(payment.amount), payment.amount) |> filter(payment.status == "paid")
However I am getting syntax error, maybe it is because of type?
I am using money_with_currency datatype for amount. I need to cast its type first before summing it to get the sum correctly. Here’s the info about it.
# Introduction to Money SQL
![Build Status](http://sweatbox.noexpectations.com.au:8080/buildStatus/icon?job=money_sql_)
[![Hex pm](http://img.shields.io/hexpm/v/ex_money.svg?style=flat)](https://hex.pm/packages/ex_money_sql)
[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://github.com/kipcole9/money_sql/blob/master/LICENSE)
Money_SQL implements a set of functions to store and retrieve data structured as a `%Money{}` type that is composed of an ISO 4217 currency code and a currency amount. See [ex_money](https://hex.pm/packages/ex_money) for details of using `Money`. Note that `ex_money_sql` depends on `ex_money`.
## Prerequisities
* `Money_SQL` is supported on Elixir 1.6 and later only
## Serializing to a Postgres database with Ecto
`Money_SQL` provides custom Ecto data types and a custom Postgres data type to provide serialization of `Money.t` types without losing precision whilst also maintaining the integrity of the `{currency_code, amount}` relationship. To serialise and retrieve money types from a database the following steps should be followed:
1. First generate the migration to create the custom type:
```elixir
mix money.gen.postgres.money_with_currency_migration
* creating priv/repo/migrations
This file has been truncated. show original