我必须找到并标记符合以下条件的客户,但我很难想出一种在SQL中使用SQL而不是在Excel中手动执行此操作的方法。
客户必须符合以下条件:
开始日期和结束日期必须相同或+1天才能解决结算延迟
我们希望标记B号汽车的销售ID#(对所有人来说都是唯一的)。
+--------+---------+----------+------------+------------+
| Cus ID | Sale ID | Motel ID | SDate: | EDate: |
+--------+---------+----------+------------+------------+
| 1 | 1234 | MA | 2017-01-01 | 2017-01-07 |
| 1 | 1435 | MB | 2017-01-07 | 2017-01-10 |
| 1 | 1562 | MA | 2017-01-10 | 2017-01-15 |
+--------+---------+----------+------------+------------+
在这里,我们将销售ID:1435标记为有效标准
+--------+---------+----------+------------+------------+
| Cus ID | Sale ID | Motel ID | SDate: | EDate: |
+--------+---------+----------+------------+------------+
| 2 | 3456 | MA | 2017-01-01 | 2017-01-07 |
| 2 | 3588 | MB | 2017-01-09 | 2017-02-15 |
| 2 | 3648 | MA | 2017-02-16 | 2017-02-17 |
+--------+---------+----------+------------+------------+
这个失败,因为#3588超过30天,不应该被标记
+--------+---------+----------+------------+------------+
| Cus ID | Sale ID | Motel ID | SDate: | EDate: |
+--------+---------+----------+------------+------------+
| 1 | 1234 | MA | 2017-01-01 | 2017-01-07 |
| 1 | 1435 | MB | 2017-01-07 | 2017-01-10 |
| 1 | 1562 | MA | 2017-01-10 | 2017-01-15 |
| 1 | 1580 | MB | 2017-01-15 | 2017-01-20 |
| 1 | 1590 | MA | 2017-01-21 | 2017-01-22 |
+--------+---------+----------+------------+------------+
这个会标记2#1435和#1580
+--------+---------+----------+------------+------------+
| Cus ID | Sale ID | Motel ID | SDate: | EDate: |
+--------+---------+----------+------------+------------+
| 1 | 4555 | MA | 2017-01-01 | 2017-01-07 |
| 1 | 4803 | MB | 2017-01-09 | 2017-01-10 |
| 1 | 5238 | MA | 2017-01-10 | 2017-01-15 |
+--------+---------+----------+------------+------------+
这个失败,因为#4555和#4803之间有2个间隔日期,不应标记
非常感谢任何建议或帮助。我可以通过Excel手动执行此操作,但这样做超过数百万或记录太耗费时间。
如果重要的话,我会使用Aginity Netezza SQL。
答案 0 :(得分:2)
您可以使用lead()
和lag()
来获取“之前”和“下一步”。我认为您的情况会转变为以下where
条件:
select t.*
from (select t.*,
lag(edate) over (partition by cusid order by sdate) as prev_edate,
lead(sdate) over (partition by cusid order by sdate) as next_sdate,
lag(motelid) over (partition by cusid order by sdate) as prev_motelid,
lead(motelid) over (partition by cusid order by sdate) as next_motelid
from t
) t
where prev_motelid = next_motelid and
prev_edate in (sdate, sdate - interval '1 day') and
next_sdate in (edate, edate + interval '1 day');
我不知道你的意思“(汽车旅馆B不能> 30天)”。如果这是中间汽车旅馆的持续时间,那么这很容易添加到条件中。