我有一张具有以下结构的表格:
create table test_table
(queuename number,
duration_sum number,
rating_sum number,
rating_avg number,
rating_cnt number
)
以下是示例数据:
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;
我需要的是以下结果:
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
我正在尝试使用pivot
它,这就是我迄今为止所写的:
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;
这是一种更好的结果方法吗?我猜使用语句可能比使用pivoting
有更好的方法。我应该说“queuename”是唯一的。pivot
union
提前致谢。