我的表格包含两列,如下所示。
Object Value
------ ------
OBJ1 200
OBJ2 300
OBJ2 400
OBJ1 100
我需要输出如下
OBJ1 OBJ2
---- -----
300 700
你能帮我解决这个问题吗?
答案 0 :(得分:3)
使用PIVOT:
Oracle安装程序:
CREATE TABLE your_table ( Object, Value ) AS
SELECT 'OBJ1', 200 FROM DUAL UNION ALL
SELECT 'OBJ2', 300 FROM DUAL UNION ALL
SELECT 'OBJ2', 400 FROM DUAL UNION ALL
SELECT 'OBJ1', 200 FROM DUAL;
<强>查询强>:
select *
from your_table
PIVOT ( SUM( value ) FOR Object IN ( 'OBJ1', 'OBJ2' ) );
<强>输出强>:
OBJ1 OBJ2
---- ----
400 700
答案 1 :(得分:2)
只是为了完整性另一种可能性:
select sum(case when object='OBJ1' then value else 0 end) as obj1,
sum(case when object='OBJ2' then value else 0 end) as obj2
from yourTable;
答案 2 :(得分:1)
试试这个
select * from
(select sum(value) Obj1 from yourTable where object= 'OBJ1 ') t1,
(select sum(value) Obj2 from yourTable where object= 'OBJ2 ') t2