根据表中与日期相关的条件按百分比聚合数据



我有一些遗留表,这些表跟踪飞行并必须提取数据。我们有三张表,分别命名为预订、航空公司和航班类型。注意,这是一个伪样本

预订:

请求日期>价格<1>欧洲<1><2>欧洲欧洲
id 客户航空公司航班类型货币
1 1 11-20-2020 10:231120
2 1 11-21-2020 10:24 1 2 110 瑞士法郎
3 2 2020年1月11日11:25 2 120 欧洲
1 2020年1月15日10:23
5 1 2020年1月11日11:2360
1 2020年1月12日10:23 1 3 35

我想查看在x天内进行多次预订的客户或在x天内使用不同货币集进行多次预订客户的总收入

您可以使用窗口函数回答这样的问题。对于第一个问题,这看起来像:

select count(distinct customer)
from (select b.*,
lag(request_date) over (partition by customer order by request_date) as prev_request_date
from booking b
) b
where request_date <= prev_request_date + interval <n> day;

最新更新