with top10 as (
select payer, count(*) as fail_tx
from flow.core.fact_transactions
where tx_succeeded = false
group by 1 having fail_tx > 100
order by 2 desc
limit 10
)
select payer, case
when error_msg like '%Error Code: 1006%' then 'Error Code: 1006'
when error_msg like '%Error Code: 1007%' then 'Error Code: 1007'
when error_msg like '%Error Code: 1055%' then 'Error Code: 1055'
when error_msg like '%Error Code: 1101%' then 'Error Code: 1101'
when error_msg like '%Error Code: 1103%' then 'Error Code: 1103'
when error_msg like '%Error Code: 1110%' then 'Error Code: 1110'
when error_msg like '%Error Code: 1205%' then 'Error Code: 1205'
else error_msg end as type, count(*) as fail_tx
from flow.core.fact_transactions
where tx_succeeded = false and payer in (select payer from top10)
group by 1, 2
order by 3 desc