I have a tendency to write this material , so when I forget how to do this, I have a simple example.
The PIVOT offer was new in 11g. Since it was 5+ years ago, I hope you use it.
Data examples
create table t ( serialno number(2,0), item_category varchar2(30), item_status varchar2(20) ); insert into t ( serialno, item_category, item_status ) select rownum serialno, ( case when rownum <= 12 then 'table' else 'chair' end ) item_category, ( case --table status when rownum <= 12 and rownum <= 6 then 'servicable' when rownum <= 12 and rownum between 7 and 9 then 'under_repair' when rownum <= 12 and rownum > 9 then 'condemned' --chair status when rownum > 12 and rownum < 13 + 10 then 'servicable' when rownum > 12 and rownum between 23 and 27 then 'under_repair' when rownum > 12 and rownum > 27 then 'condemned' end ) item_status from dual connect by level <= 30; commit;
and PIVOT request:
select * from ( select item_status stat, item_category, item_status from t ) pivot ( count( item_status ) for stat in ( 'servicable' as "servicable", 'under_repair' as "under_repair", 'condemned' as "condemned" ) ); ITEM_CATEGORY servicable under_repair condemned
I still prefer the @Ramblin 'Man way to do this (other than using CASE instead of DECODE).
Edit
I just realized that I left the TOTAL column. I'm not sure there is a way to get this column using the PIVOT clause, maybe someone else knows how to do this. It may also be the reason that I do not use it often.
source share