My data looks as below
My data table looks like this:
client_id Status
1 a
1 b
1 d
1 e
2 a
2 d
3 a
3 b
3 e
4 a
4 d
5 b
I need to group by client where status has values both a and d
The result set from above should return
client_id a_d_Flag
1 Yes
2 Yes
3 No
4 Yes
5 No
Query :
SELECT c.client
, CASE WHEN c.Status = 'a' AND c.status = 'd'
THEN 'Yes'
ELSE 'No'
END AS a_d_Flag
FROM client_details c
GROUP BY c.client
HAVING c.Status = 'a' AND c.status = 'd'
2条答案
按热度按时间vd8tlhqk1#
use a case expression to check for
Status
and count fora
ord
That is assuming that you does not have duplicate
Status
for aclient_id
.If you have such as like
change the
count
tocount(distinct case ...)
k5ifujac2#
You can use conditional aggregation
Note that this method handles duplicate rows of
a
ord
correctly.