Try this (remove top orange section, as this is just sample data I provided for test, and also replace buttom orange section with the correct tablename):with yourtable(id,merchantname,accountnum,fraud) as ( select 1,'A',1,NULL union all select 2,'A',2,NULL union all select 3,'A',3,'y' union all select 4,'A',4,NULL union all select 5,'A',5,'y' union all select 6,'A',3,'y' union all select 7,'A',6,NULL union all select 8,'B',1,NULL union all select 9,'B',2,NULL union all select 10,'B',3,'y' union all select 11,'C',2,NULL union all select 12,'C',4,NULL union all select 13,'C',5,'y' )select merchantname ,sum(accounts) as accounts ,sum(account_frauds) as account_frauds ,sum(transactions) as transactions ,sum(transaction_frauds) as transaction_frauds from (select merchantname ,1 as accounts ,sign(sum(case when fraud in ('y','Y') then 1 else 0 end)) as account_frauds ,count(*) as transactions ,sum(case when fraud in ('y','Y') then 1 else 0 end) as transaction_frauds from yourtable group by merchantname ,accountnum ) as a group by merchantname order by merchantname