I want to subgroup my PK1 and PK2 fields based on the four flags I have. If I have any of flags as blank then the results should be Yes for that PK1. If all the four flags in all PK2 for that particular PK1 is X then it should return no.
PK1 PK2 Flag1 Flag2 Flag3 Flag4
100 10 X
100 20 X
100 30
200 40 X X X X
200 50 X X X X
300 A10 X
300 A20 X
The expected Output is :
100 Yes
200 No ( as all flag values for each PK2 is X)
300 Yes
Any pointers how should I solve this in SQL Regards,
You can try the below -
select pk1, case when max(flag1) is not null and max(flag2) is not null and max(flag3) is not null and max(flag4) is not null then 'No' else 'Yes' end as flagval
from tablename
group by pk1
SELECT pk1, CASE SUM(flag1='' + flag2='' + flag3='' + flag4='')
WHEN 0
THEN 'No'
ELSE 'Yes'
END
FROM table
GROUP BY pk1
The query assumes that none flag value is NULL.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.