与GROUP BY一起求和数组

时间:2017-12-07 20:06:41

标签: postgresql

我已经从不同的对象获得了一些周期性的计数器数据(比如每秒一次),我希望将它们合并为一小时的总数。

如果我使用单独的列名称,它非常简单:

CREATE TABLE ts1 (
        id      INTEGER,
        ts      TIMESTAMP,
        count0  integer,
        count1  integer,
        count2  integer
);

INSERT INTO ts1 VALUES
        (1, '2017-12-07 10:37:48', 10, 20, 50),
        (2, '2017-12-07 10:37:48', 13, 7, 88),
        (1, '2017-12-07 10:37:49', 12, 23, 34),
        (2, '2017-12-07 10:37:49', 11, 13, 46),
        (1, '2017-12-07 10:37:50', 8, 33, 80),
        (2, '2017-12-07 10:37:50', 9, 3, 47),
        (1, '2017-12-07 10:37:51', 17, 99, 7),
        (2, '2017-12-07 10:37:51', 9, 23, 96);

SELECT id, date_trunc('hour', ts + '1 hour') nts, 
           sum(count0), sum(count1), sum(count2) 
    FROM ts1 GROUP BY id, nts;

id |         nts         | sum | sum | sum
----+---------------------+-----+-----+-----
  1 | 2017-12-07 11:00:00 |  47 | 175 | 171
  2 | 2017-12-07 11:00:00 |  42 |  46 | 277
(2 rows)

问题是不同的对象具有不同的计数(尽管每个特定对象的行 - 共享相同ID的行 - 都具有相同的计数数)。因此我想使用数组。

相应的表格如下:

CREATE TABLE ts2 (
        id      INTEGER,
        ts      TIMESTAMP,
        counts  INTEGER[]
);

INSERT INTO ts2 VALUES
        (1, '2017-12-07 10:37:48', ARRAY[10, 20, 50]),
        (2, '2017-12-07 10:37:48', ARRAY[13, 7, 88]),
        (1, '2017-12-07 10:37:49', ARRAY[12, 23, 34]),
        (2, '2017-12-07 10:37:49', ARRAY[11, 13, 46]),
        (1, '2017-12-07 10:37:50', ARRAY[8, 33, 80]),
        (2, '2017-12-07 10:37:50', ARRAY[9, 3, 47]),
        (1, '2017-12-07 10:37:51', ARRAY[17, 99, 7]),
        (2, '2017-12-07 10:37:51', ARRAY[9, 23, 96]);

我已经看过这个答案https://stackoverflow.com/a/24997565/1076479,我得到了它的一般要点,但是当我尝试将它与id和timestamp的分组结合起来时,我无法弄清楚如何将正确的行汇总在一起。

例如,通过这个,我获得所有行,而不仅仅是具有匹配id和时间戳的行:

SELECT id, date_trunc('hour', ts + '1 hour') nts, ARRAY(
        SELECT sum(elem) FROM ts2 t, unnest(t.counts)
            WITH ORDINALITY x(elem, rn) GROUP BY rn ORDER BY rn
        ) FROM ts2 GROUP BY id, nts;

 id |         nts         |    array
----+---------------------+--------------
  1 | 2017-12-07 11:00:00 | {89,221,448}
  2 | 2017-12-07 11:00:00 | {89,221,448}
(2 rows)

FWIW,我正在使用postgresql 9.6

1 个答案:

答案 0 :(得分:1)

原始查询的问题在于您要对所有元素求和,因为GROUP BY id, nts在外部查询中执行。将CTELATERAL JOIN结合起来可以解决问题:

WITH tmp AS (
    SELECT 
        id, 
        date_trunc('hour', ts + '1 hour') nts, 
        sum(elem) AS counts
    FROM 
        ts2 
        LEFT JOIN LATERAL unnest(counts) WITH ORDINALITY x(elem, rn) ON TRUE 
    GROUP BY 
        id, nts, rn
)
SELECT id, nts, array_agg(counts) FROM tmp GROUP BY id, nts