儘管@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)
你正在使用什麼數據庫? – 2013-05-08 21:32:15
Postgresql 9.2.3 – kwyoung11 2013-05-08 21:32:47