我有一张表格,其中包含每所学校的所有购买。我可以获得每天每所学校,每个项目, 与以下。
SELECT
date
school_id,
item_id,
sum(price) as total_price
FROM purchases
GROUP BY school_id, item_id, date
ORDER BY school_id, date
它将返回类似
的内容date school_id item_id total_price
2016-11-18 | 1 | 1 | 0.50
2016-11-17 | 1 | 2 | 1.00
2016-11-16 | 1 | 1 | 0.50
2016-11-18 | 2 | 2 | 1.00
2016-11-17 | 2 | 2 | 1.00
2016-11-16 | 2 | 2 | 1.00
我需要一个表格,该表格返回每天最近3天(包括当天)的总价, 所以像
date school_id item_id total_price
2016-11-18 | 1 | 1 | 1.00
2016-11-17 | 1 | 2 | 1.00
2016-11-16 | 1 | 1 | 0.50
2016-11-18 | 2 | 2 | 3.00
2016-11-17 | 2 | 2 | 2.00
2016-11-16 | 2 | 2 | 1.00
我知道我可以使用lag()OVER(PARTITION BY),但是我可能需要在几个月内而不是3天的时间内执行此操作,并且lag会花很多时间才能设置。 我不确定我可以使用其他什么方法。有指导吗?
答案 0 :(得分:1)
一个简单的INNER JOIN就可以了 当学校和项目匹配,并且日期在3天范围内时,您将表格与自身连接起来
请注意,这将给出最近3天的移动平均值,但是从您的问题来看,似乎如此,因为您希望连续几天没有跳跃
:id
答案 1 :(得分:1)
一种方法是只在select子句中使用相关的子查询:
SELECT
date,
school_id,
item_id,
(SELECT SUM(p2.price) FROM purchases p2
WHERE p1.school_id = p2.school_id AND
p2.date BETWEEN p1.date - INTERVAL '3 DAY' AND p1.date) AS total_price
FROM purchases p1
GROUP BY school_id, item_id, date
ORDER BY school_id, date DESC;
另一种方法是利用Postgres的窗口功能:
SELECT
date,
school_id,
item_id,
SUM(price) OVER (PARTITION BY school_id
ORDER BY date ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) AS total_price
FROM purchases p1
GROUP BY school_id, item_id, date
ORDER BY school_id, date DESC;
两者都生成此输出:
请注意,我的school_id=1
输出与您的预期输出不一致,但是我认为您的预期数据有错字。