我有一些遗留表,这些表跟踪飞行并必须提取数据。我们有三张表,分别命名为预订、航空公司和航班类型。注意,这是一个伪样本
预订:
id | 客户 | 请求日期航空公司 | 航班类型 | >价格货币 | |||
---|---|---|---|---|---|---|---|
1 | 1 | 11-20-2020 10:23 | <1>1 | 120 | 欧洲|||
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:23 | <1><2>60 | 欧洲||||
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;