I have a dataset like the following:
ID|Date_Val|Data
1|2022-01-01|A
1|2022-01-01|I
1|2022-01-01|H
2|2022-01-01|G
2|2022-01-01|G
2|2022-01-01|I
I would like to run a query like the following:
SELECT ID, Date_Val, IF(/logic here/, 'A', 'B')
GROUP BY 1,2
Output dataset
ID|Date_Val|Data
1|2022-01-01|A
2|2022-01-01|B
How would I write /logic here/ so that if any Data value in the grouping (ID, Date_Val) is = 'A' then 'A' else 'B'.
>Solution :
We can try:
SELECT ID, Date_Val,
CASE WHEN MAX(CASE WHEN Data = 'A' THEN 1 END) > 0 THEN 'A' ELSE 'B' END AS Data
FROM yourTable
GROUP BY 1, 2;