[ad_1]
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;
[ad_2]
solved Very complicated SQL query