[Solved] Very complicated SQL query


You can do aggregation with coalesce() :

select event_id,
       coalesce(max(case when state="FAILED" then 'FAILED' end),
                max(case when state="COMPLETED" then 'COMPLETED' end),
                'PENDING'     
               )
from table t
group by event_id;

solved Very complicated SQL query