Sample Header Ad - 728x90

Better approach to 'pivot' the data of a table

0 votes
1 answer
59 views
I have a table with below structure: create table test_table (queuename number, duration_sum number, rating_sum number, rating_avg number, rating_cnt number ) Here is the sample data: insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt) values (1000,50,40,60,70); insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt) values (1010,12,40,25,34); insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt) values (2000,50,34,60,23); insert into test_table (queuename, duration_sum, rating_sum, rating_avg,rating_cnt) values (3000,90,40,60,67); commit; What I need is below result: queuename 1000 1010 2000 3000 duration_sum 50 12 50 90 rating_sum 40 40 34 40 rating_avg 60 25 60 60 rating_cnt 70 34 23 67 Im trying to use pivot for this and this is what I have written so far: select * from ( select queuename, 'duration_sum' as measure, duration_sum as value from test_table union all select queuename, 'rating_sum' as measure, rating_sum as value from test_table union all select queuename, 'rating_avg' as measure, rating_avg as value from test_table union all select queuename, 'rating_cnt' as measure, rating_cnt as value from test_table ) pivot ( max(value) for queuename in (1000 as "1000", 1010 as "1010", 2000 as "2000", 3000 as "3000") ) order by measure; I this a better approach for pivoting the result? I guess there might be a better approach for using pivot statement rather than using union. I should say that 'queuename' is unique. Thanks in advance.
Asked by Pantea (1510 rep)
Dec 13, 2024, 08:45 AM
Last activity: Dec 13, 2024, 09:29 AM