Oracle SQL枢轴查询我有如下表所示的数据:MONTH VALUE1 1002 2003 3004 4005 5006 600我想编写一个SQL查询,以便给出结果如下:MONTH_JAN MONTH_FEB MONTH_MAR MONTH_APR MONTH_MAY MONTH_JUN100 200 300 400 500 600
2 回答
白衣染霜花
TA贡献1796条经验 获得超10个赞
Oracle 9i+支持:
SELECT SUM(CASE WHEN t.month = 1 THEN t.value ELSE 0 END) AS JAN, SUM(CASE WHEN t.month = 2 THEN t.value ELSE 0 END) AS FEB, SUM(CASE WHEN t.month = 3 THEN t.value ELSE 0 END) AS MAR, SUM(CASE WHEN t.month = 4 THEN t.value ELSE 0 END) AS APR, SUM(CASE WHEN t.month = 5 THEN t.value ELSE 0 END) AS MAY, SUM(CASE WHEN t.month = 6 THEN t.value ELSE 0 END) AS JUN FROM YOUR_TABLE t
月关宝盒
TA贡献1772条经验 获得超5个赞
Oracle 11g及以上
PIVOT
create table tq84_pivot ( month number, value number);insert into tq84_pivot values(1, 100);insert into tq84_pivot values(2, 200);insert into tq84_pivot values(3, 300); insert into tq84_pivot values(4, 400);insert into tq84_pivot values(5, 500);insert into tq84_pivot values(6, 600); --insert into tq84_pivot values(1, 400);insert into tq84_pivot values(2, 350);insert into tq84_pivot values(4, 150);select *from tq84_pivotpivot ( sum (value) as sum_value for (month) in (1 as month_jan, 2 as month_feb, 3 as month_mar, 4 as month_apr, 5 as month_mai, 6 as month_jun, 7 as month_jul, 8 as month_aug, 9 as month_sep, 10 as month_oct, 11 as month_nov, 12 as month_dec));
添加回答
举报
0/150
提交
取消