带有 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
对于给定的客户 ID,
如果决定是已批准的,则只保留客户批准的行。如果客户没有任何已批准的决定,则保留客户的所有行。
期待输出
Customer Decision req_date
A Approved 2017-06-13
A Approved 2017-05-13
B Approved 2017-06-13
C Pending 2017-07-14
我会用 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'
);
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
对于给定的客户 ID,
如果决定是已批准的,则只保留客户批准的行。如果客户没有任何已批准的决定,则保留客户的所有行。
期待输出
Customer Decision req_date
A Approved 2017-06-13
A Approved 2017-05-13
B Approved 2017-06-13
C Pending 2017-07-14
我会用 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'
);