I'm looking to create a computed column in Hasura which returns a set of another table and includes a running total column of the set. For example:
table_a
id product_id
----------- ----------
1 "1"
2 "2"
2 "3"
table_b
id product_id qty created_at
----------- ---------- --- ----------
1 "1" 6 01/01/20
2 "2" 4 01/02/20
3 "3" 2 01/02/20
4 "3" 2 01/02/20
5 "1" 4 01/03/20
6 "2" 6 01/03/20
Desired GQL Response:
{
"data": {
"table_a": [
{
"id": 1,
"product_id": "1",
"computed_table_b_rows": [
{
id: 1,
product_id: "1",
qty: 6,
created_at: 01/01/20,
running_total: 6,
},
{
id: 5,
product_id: "1",
qty: 4,
created_at: 01/03/20,
running_total: 10,
},
]
}
]
}
}
Here's what I have so far which does not work:
CREATE FUNCTION filter_table_b(table_a_row table_a)
RETURNS SETOF table_b AS $$
SELECT *,
SUM (qty) OVER (PARTITION BY product_id ORDER BY created_at) as running_total
FROM table_b
WHERE product_id = table_a_row.product_id
$$ LANGUAGE sql STABLE;