Home > Back-end >  SQL group data into an array
SQL group data into an array

Time:05-25

I have this query wich works:

select point_delivery_number, year(time - -120m) as year, month(time - -120m) - 1 as month, SUM(consumption) as value from "energy_datapoints"."formatted_raw"
WHERE point_delivery_number in ('AT523452345sadf345', 'AT2341234asdf5234452341243')
GROUP BY 1,2,3
ORDER BY year, month
LIMIT 24

It outputs me this result:

enter image description here

I want to group this by its point_delivery_number

Result should be

point_delivery_number | year | data 
------------------------------------------------------
AT23...               | 2021 | [216.54, 202.77, 210.4]
------------------------------------------------------
At523..               | 2021 | [489.84, 423.6, ...]

I tried it with ARRAY_AGG

select point_delivery_number, year(time - -120m) as year, month(time - -120m) - 1 as month, ARRAY_AGG(SUM(consumption)) as data from "energy_datapoints"."formatted_raw"
WHERE point_delivery_number in ('AT523452345sadf345', 'AT2341234asdf5234452341243')
GROUP BY 1,2,3
ORDER BY year, month
LIMIT 24

But it says :

Cannot nest aggregations inside aggregation 'ARRAY_AGG'

I am using aws timestream database, so there are some limitations. Some functions does not even exist.

CodePudding user response:

I actually found it out by myself. I made a subquery:

select point_delivery_number, array_agg(value) as value from 
(select point_delivery_number, year(time - -120m) as year, month(time - -120m) - 1 as month, SUM(consumption) as value from "energy_datapoints"."formatted_raw"
WHERE point_delivery_number in ('AT523452345sadf345', 'AT2341234asdf5234452341243')
GROUP BY 1,2,3
ORDER BY year, month
LIMIT 24)
group by point_delivery_number

CodePudding user response:

The error is clear, you can't do aggregations inside the ARRAY_AGG function. One possible solution is to introduce a sub-query block, such as:

WITH sub_query AS (
  SELECT
    point_delivery_number,
    year(time - -120 m) as year,
    month(time - -120 m) - 1 as month,
    SUM(consumption) as value
  FROM
    "energy_datapoints"."formatted_raw"
  WHERE
    point_delivery_number in ('AT523452345sadf345', 'AT2341234asdf5234452341243')
  GROUP BY
    1, 2, 3
)

SELECT
  point_delivery_number,
  year,
  month,
  ARRAY_AGG(value)
FROM
  sub_query
GROUP BY
  1, 2, 3
ORDER BY
  2, 3

WITH statements are very useful to handle this kind of problem.

  • Related