with t(a,b,c,d,e) as ( select 'DATA1','DATA2','DATA1','DATA4','DATA1' from dual union all select 'DATA2','DATA4','DATA2','DATA3','DATA3' from dual union all select 'DATA3','DATA5','DATA3','DATA2','DATA5' from dual union all select 'DATA5','DATA1','DATA4','DATA1','DATA2' from dual ) select decode(lv,1,'A',2,'B',3,'C',4,'D',5,'E') gb , nvl(max(decode(data,'DATA1',1)),0) data1 , nvl(max(decode(data,'DATA2',1)),0) data2 , nvl(max(decode(data,'DATA3',1)),0) data3 , nvl(max(decode(data,'DATA4',1)),0) data4 , nvl(max(decode(data,'DATA5',1)),0) data5 from ( select lv, decode(lv,1,a,2,b,3,c,4,d,5,e) data from t, (select level lv from dual connect by level <=5) ) group by lv order by gb
with t(a,b,c,d,e) as ( select 'DATA1','DATA2','DATA1','DATA4','DATA1' from dual union all select 'DATA2','DATA4','DATA2','DATA3','DATA3' from dual union all select 'DATA3','DATA5','DATA3','DATA2','DATA5' from dual union all select 'DATA5','DATA1','DATA4','DATA1','DATA2' from dual ) select GB , COUNT(CASE WHEN DT = 'DATA1' THEN 1 END) DATA1 , COUNT(CASE WHEN DT = 'DATA2' THEN 1 END) DATA2 , COUNT(CASE WHEN DT = 'DATA3' THEN 1 END) DATA3 , COUNT(CASE WHEN DT = 'DATA4' THEN 1 END) DATA4 , COUNT(CASE WHEN DT = 'DATA5' THEN 1 END) DATA5 from ( select decode(lv,1,a,2,b,3,c,4,d,5,e) dt , decode(lv,1,'A',2,'B',3,'C',4,'D',5,'E') GB from t, (select level lv from dual connect by level <= 5)) GROUP BY GB ORDER BY GB;