I have a situation where I need to use DISTINCT to get unique values. But there is additional condition. In the duplicate rows I get some have a value SUCCESS and some have value FAILURE. I need it so that in the DISTINCT result, if any of the duplicates has a FAILURE then the DISTINCT result will have a failure, else it will be SUCCESS. How can i achieve this ?
SELECT DISTINCT COL1, COL2, COL3, Result (
Select COL1, COL2, COL3, CASE WHEN resolved THEN 'SUCCESS' ELSE 'FAILURE' END as Result FROM table
UNION
// Some other queries with similar results
)
3
Answers
You can achieve that with
GROUP BY
:You can count ‘FAILURE’s directly.
See example
You can use the
distinct on
clause and order the result according to the boolean valuedb fiddle