Для более поздних читателей здесь есть еще одно решение https://technology.amis.nl/2006/05/24/dynamic-sql-pivoting-stealing-antons-thunder/
, разрешающий запрос типа
select * from table( pivot( 'select deptno, job, count(*) c from scott.emp group by deptno,job' ) )