# Calculations implementation in Postgres

**URL:** <https://forum.elixirforum.com/t/calculations-implementation-in-postgres/60776>\
**Category:** Questions\
**Tags:** ash, ash-postgres, ash-resource-calculations\
**Created:** [January 4, 2024, 9:40pm UTC](https://forum.elixirforum.com/t/calculations-implementation-in-postgres/60776 "2024-01-04T21:40:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BryanJBryce](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bryanjbryce/32/11560_2.png) [@BryanJBryce](https://forum.elixirforum.com/u/BryanJBryce)\
**Post date:** [January 4, 2024, 9:40pm UTC](https://forum.elixirforum.com/t/calculations-implementation-in-postgres/60776/1 "2024-01-04T21:40:27Z")

</div>

When a resource is backed by Postgres are Ash Calculations implemented as generated columns?

---

<div class="post-metadata">

**Author:** ![couhajjou](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/couhajjou/32/33230_2.png) [@couhajjou](https://forum.elixirforum.com/u/couhajjou)\
**Post date:** [January 4, 2024, 9:57pm UTC](https://forum.elixirforum.com/t/calculations-implementation-in-postgres/60776/2 "2024-01-04T21:57:18Z")

</div>

The calculations are translated to Postgres SQL code that does the computation.

here is an example:

```elixir
  calculations do
    calculate :amount, AshMoney.Types.Money, expr(quantity * 10 / 4 * price)
  end

```

and here is the sql generated when the calculation is loaded:

`SELECT d0."price", d0."quantity", (((d0."quantity"::decimal * $1)::decimal / $2::decimal) * d0."price"::money_with_currency)::money_with_currency::money_with_currency FROM "demo_orders" AS d0 LIMIT $3 [Decimal.new("10"), Decimal.new("4"), 1000]`

---

<div class="post-metadata">

**Author:** ![barnabasJ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/barnabasj/32/29318_2.png) [@barnabasJ](https://forum.elixirforum.com/u/barnabasJ)\
**Post date:** [January 4, 2024, 9:58pm UTC](https://forum.elixirforum.com/t/calculations-implementation-in-postgres/60776/3 "2024-01-04T21:58:27Z")

</div>

If you use `expr` for your calculations they can be translated to SQL. If you use a module calculation they are done in Elixir after loading the data.

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [January 4, 2024, 10:08pm UTC](https://forum.elixirforum.com/t/calculations-implementation-in-postgres/60776/4 "2024-01-04T22:08:59Z")

</div>

A subset of calculations (ones that don’t require joins) may eventually be lowered to be generated columns automatically by ash\_postgres.
