TableA:
date_time amount note
2016-03-01 01:00.00.000000 100 "hi"
2016-03-01 02:00.00.000000 5 "hello"
2016-03-01 03:00.00.000000 2 "foo"
2016-04-01 00:00.00.000000 60 "bar"
I need to output the final table like this:
ProcessedTable
grouped_date_time row total
2016-03-01 RowA 107
2016-04-01 RowB 60
RowA is an array of JSON objects from all rows in the first group:
[
{ date_time:2016-03-01 01:00.00.000000, amount:100, note:"hi"},
{ date_time:2016-03-01 02:00.00.000000, amount:5, note:"hello"},
{ date_time:2016-03-01 03:00.00.000000, amount:2, note:"foo"}
]
RowB is an array of JSON objects from all rows in the first group:
[
{ date_time:2016-04-01 00:00.00.000000, amount:60, note:"bar"}
]
Currently I have query like this
SELECT date_trunc('day', date_time), array_agg(amount)
FROM table_a
GROUP BY date_trunc('day', date_time)
I am not sure what to put for array_agg(amount) to extract out the whole JSON object as a column, and the total amount for each group.