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

时间:2018-08-13 21:49:02

标签: sql postgresql plsql plsqldeveloper

    @SuppressLint("ValidFragment")
public ProductsFragment(String id) {
    categoryID = id;
}

对于给定的客户ID,

如果批准该决定,则仅保留该客户的批准行。如果该客户没有任何批准的决定,则保留该客户的所有行。

期望输出

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

1 个答案:

答案 0 :(得分:2)

我会使用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'
                 );