简体   繁体   English

带有in group by或partition by子句的case语句

[英]case statement with in group by or partition by clause

Customer   Decision      req_date   
   A       Approved     2017-06-13
   A       Approved     2017-06-13
   A       Pending      2017-06-13
   B       Pending      2017-10-13
   B       Approved     2017-06-13
   C       Pending      2017-07-14

For a given customer ID, 对于给定的客户ID,

If the decision is Approved, retain only the approved row for the customer .If the customer doesn't have any Approved decision then retain all the rows of the customer. 如果决策被批准,则仅保留客户的已批准行。如果客户没有任何批准的决策,则保留客户的所有行。

expecting output 期望的输出

Customer   Decision      req_date   
   A       Approved     2017-06-13
   A       Approved     2017-05-13
   B       Approved     2017-06-13
   C       Pending      2017-07-14

I would use or : 我会使用or

select t.*
from t
where t.decision = 'Approved' or
      not exists (select 1
                  from t t2
                  where t2.Customer = t.Customer and t2.decision = 'Approved'
                 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM