分组时如何将条件限制为CASE中的数据子集?

时间:2019-03-06 08:21:03

标签: sql snowflake-datawarehouse snowflake

假设表格:

| ID | Customer | Agent    | Event     | Date       |
|====|==========|==========|===========|============|
| 01 | Steve    | Jerry    | Booked    | 2019-01-01 |
| 02 | Steve    | Jerry    | Canceled  | 2019-01-13 |
| 03 | Steve    | Rusty    | Booked    | 2019-01-16 |
| 04 | Carla    | JSnow    | Booked    | 2019-01-04 |
| 05 | Carla    | JSnow    | Resched   | 2019-01-16 |

我有一个查询,可将事件列表分组为其所有者,检测第一个预订日期,并记下第一次预订的代理商:

SELECT 
    Customer,
    FIRST_VALUE(Agent),
    MIN(CASE WHEN Event = 'Booked' THEN Date END) as BookDate,
    MIN(CASE WHEN Event = 'Canceled' THEN Date END) as CancelDate
FROM Events
GROUP BY Customer

// produces:
| Customer | Agent    | BookDate   | CancelDate |
|==========|==========|============|============|
| Steve    | Jerry    | 2019-01-01 | 2019-01-13 |
| Carla    | JSnow    | 2019-01-04 | NULL       |

我正在尝试添加“取消后的Booking Agent”列,使其看起来像这样:

| Customer | Agent    | BookDate   | CancelDate | Agent2   |
|==========|==========|============|============|==========|
| Steve    | Jerry    | 2019-01-01 | 2019-01-13 | Rusty    |
| Carla    | JSnow    | 2019-01-04 | NULL       | NULL     |

我不知道如何限制座席姓名的汇总,使其仅包括“预订”日期超过“取消”日期(如果存在)的事件。

1 个答案:

答案 0 :(得分:2)

我认为仅通过聚合就没有任何方法可以做到这一点。但是在子查询中添加LAG()可以为您提供所需的信息:

SELECT Customer, FIRST_VALUE(Agent),
       MIN(CASE WHEN Event = 'Booked' THEN Date END) as BookDate,
       MIN(CASE WHEN Event = 'Canceled' THEN Date END) as CancelDate,
       MIN(CASE WHEN prev_event = 'Canceled' THEN Agent END) as Agent2
FROM (SELECT e.*, 
             LAG(Event) OVER (PARTITION BY Customer ORDER BY Date) as prev_event
      FROM Events e
     ) e
GROUP BY Customer;