尽管@flaschenpost 和@Gordon Linoff 的答案非常有帮助,但我最终需要两个答案的各个方面。
以下是我的查询结果:
SELECT array_agg(id) OVER (PARTITION BY date(created_at)) as ids, date(created_at), sum(load_volume) OVER (PARTITION BY date(created_at)) as total_load_volume
FROM "workouts" WHERE "workouts"."user_id" = 5 AND "workouts"."category" = 'All'
GROUP BY date(created_at), id, load_volume ORDER BY date(created_at) ASC;
要获取每个锻炼 ID,如果有多个锻炼,在给定日期我需要使用 array_agg 以及窗口函数。这是输出:
ids | date | total_load_volume
------------------------------------------------+------------+-------------------
{30} | 2013-04-20 | 400
{29} | 2013-04-23 | 400
{31} | 2013-04-24 | 400
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{33,34,35,36,37,38,41,42,43,44,45,46,47,48,49} | 2013-04-28 | 1732
{50} | 2013-04-30 | 400
{51} | 2013-05-07 | 400
(20 rows)