简体   繁体   English

如何按时间范围内的组进行分区?

[英]How can I partition by group that falls within a time range?

I have the following table showing when customers bought a certain product.我有下表显示客户何时购买某种产品。 The data I have is CustomerID, Amount, Dat .我拥有的数据是CustomerID, Amount, Dat I am trying to create the column ProductsIn30Days , which represents how many products a customer bought in the range Dat-30 days inclusive the current day.我正在尝试创建列ProductsIn30Days ,它表示客户在当天的Dat-30天范围内购买了多少产品。

For example, ProductsIn30Days for CustomerID 1 on Dat 25.3.2020 is 7, since the customer bought 2 products on 25.3.2020 and 5 more products on 24.3.2020, which falls within 30 days before 25.3.2020.例如, ProductsIn30DaysCustomerID 1 Dat 2020年3月25日为7,因为客户购买的产品2在2020年3月25日和5更多的产品上2020年3月24日,2020年3月25日之前的30天内落在。

CustomerID顾客ID Amount数量 Dat数据 ProductsIn30Days产品在30天
1 1 1 1 23.3.2018 23.3.2018 1 1
1 1 2 2 24.3.2020 24.3.2020 2 2
1 1 3 3 24.3.2020 24.3.2020 5 5
1 1 2 2 25.3.2020 25.3.2020 7 7
1 1 2 2 24.5.2020 24.5.2020 2 2
1 1 1 1 15.6.2020 15.6.2020 3 3
2 2 7 7 24.3.2017 24.3.2017 7 7
2 2 2 2 24.3.2020 24.3.2020 2 2

I tried something like this with no success, since the partition only works on a single date rather than on a range like I would need:我尝试了这样的事情但没有成功,因为分区只能在单个日期上工作,而不是在我需要的范围内工作:

select CustomerID, Amount, Dat,
sum(Amount) over (partition by CustomerID, Dat-30)
from table

Thank you for help.谢谢你的帮助。

You can use an analytic SUM function with a range window:您可以使用带有范围窗口的解析SUM函数:

SELECT t.*,
       SUM(Amount) OVER (
         PARTITION BY CustomerID
         ORDER BY Dat
         RANGE BETWEEN INTERVAL '30' DAY PRECEDING AND CURRENT ROW
       ) AS ProductsIn30Days
FROM   table_name t;

Which, for the sample data:其中,对于样本数据:

CREATE TABLE table_name (CustomerID, Amount, Dat) AS
SELECT 1, 1, DATE '2018-03-23' FROM DUAL UNION ALL
SELECT 1, 2, DATE '2020-03-24' FROM DUAL UNION ALL
SELECT 1, 3, DATE '2020-03-24' FROM DUAL UNION ALL
SELECT 1, 2, DATE '2020-03-25' FROM DUAL UNION ALL
SELECT 1, 2, DATE '2020-05-24' FROM DUAL UNION ALL
SELECT 1, 1, DATE '2020-06-15' FROM DUAL UNION ALL
SELECT 2, 7, DATE '2017-03-24' FROM DUAL UNION ALL
SELECT 2, 2, DATE '2020-03-24' FROM DUAL;

Outputs:输出:

CUSTOMERID顾客ID AMOUNT数量 DAT数据 PRODUCTSIN30DAYS产品30天
1 1 1 1 2018-03-23 00:00:00 2018-03-23 00:00:00 1 1
1 1 2 2 2020-03-24 00:00:00 2020-03-24 00:00:00 5 5
1 1 3 3 2020-03-24 00:00:00 2020-03-24 00:00:00 5 5
1 1 2 2 2020-03-25 00:00:00 2020-03-25 00:00:00 7 7
1 1 2 2 2020-05-24 00:00:00 2020-05-24 00:00:00 2 2
1 1 1 1 2020-06-15 00:00:00 2020-06-15 00:00:00 3 3
2 2 7 7 2017-03-24 00:00:00 2017-03-24 00:00:00 7 7
2 2 2 2 2020-03-24 00:00:00 2020-03-24 00:00:00 2 2

Note: If you have values on the same date then they will be tied in the order and always aggregated together (ie rows 2 & 3).注意:如果您在同一日期有值,那么它们将按顺序绑定并始终聚合在一起(即第 2 行和第 3 行)。 If you want them to be aggregated separately then you need to order by something else to break the ties but that would not work with a RANGE window.如果您希望它们单独聚合,那么您需要通过其他方式订购以打破联系,但这不适用于RANGE窗口。

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM