In Supabase I have two tables, invoices
and invoice_items
, where invoice_items
have a total
column which represents the total for that item on an invoice, i.e. includes tax
Im trying to write a query that returns all invoices, with a calculated total for each invoice by running sum()
on the invoice’s item’s totals
With an example of a single invoice, id = 1, and two invoice_items with totals of 5000 and 1300
Ive tried a few things but im currently at –
<code>await supabase
.from("invoices")
.select(
`
id,
total:invoice_items(sum:total.sum())
`,
)
</code>
<code>await supabase
.from("invoices")
.select(
`
id,
total:invoice_items(sum:total.sum())
`,
)
</code>
await supabase
.from("invoices")
.select(
`
id,
total:invoice_items(sum:total.sum())
`,
)
which feels close, but the result is in the format –
<code>[{
"id": '1',
"total":[{
"sum":6300
}]
}]
</code>
<code>[{
"id": '1',
"total":[{
"sum":6300
}]
}]
</code>
[{
"id": '1',
"total":[{
"sum":6300
}]
}]
but im trying to achieve (if possible) –
<code>[{
"id": '1',
"total": 6300
}]
</code>
<code>[{
"id": '1',
"total": 6300
}]
</code>
[{
"id": '1',
"total": 6300
}]
Is it possible, and any ideas on what I’m doing wrong?