查询以列出分组中不存在值的位置

时间:2018-09-18 18:41:54

标签: sql select

寻找一种方法来报告SQL中“ PRO_BILL”列表中不存在“ delivery_completed”更新代码的地方

-非常抱歉,一个新手-

pro_bill    | update_code           | entry_timestamp
PRO0211475  | delivery_arrived      | 6/14/18 12:23
PRO0211475  | delivery_completed    | 6/14/18 12:23
PRO0211475  | pick_up_arrived       | 6/14/18 12:23
PRO0211475  | pick_up_completed     | 6/14/18 12:23
PRO0211510  | pick_up_completed     | 6/14/18 11:52
PRO0211510  | pick_up_arrived       | 6/14/18 11:52
PRO0211510  | delivery_arrived      | 6/14/18 11:52
PRO0211527  | delivery_arrived      | 6/14/18 11:27
PRO0211527  | pick_up_arrived       | 6/14/18 11:27
PRO0211527  | pick_up_completed     | 6/14/18 11:27

理想情况下,我希望查询返回(在一行中),其中该pro_bill的delivery_departed不存在

Pro_bill | pick_up_arrived | pick_up_completed | delivery_arrived

1 个答案:

答案 0 :(得分:0)

您可能需要条件聚合

 SELECT 
   pro_bill,
   MAX( CASE WHEN update_code = 'delivery_arrived' THEN entry_timestamp END) as delivery_arrived,
   MAX( CASE WHEN update_code = 'delivery_completed' THEN entry_timestamp END) as delivery_completed,
   MAX( CASE WHEN update_code = 'pick_up_arrived' THEN entry_timestamp END) as pick_up_arrived,
   MAX( CASE WHEN update_code = 'pick_up_completed' THEN entry_timestamp END) as pick_up_completed
FROM YourTable
GROUP BY pro_bill