这就是我们通常的做法:
SQL> with temp (gbc, id, value) as
2 -- sample data
3 (select 'a', 1, 50 from dual union all
4 select 'b', 1, 15 from dual union all
5 select 'c', 1, 34 from dual union all
6 --
7 select 'a', 2, 30 from dual union all
8 select 'b', 2, 10 from dual union all
9 select 'c', 2, 30 from dual union all
10 --
11 select 'a', 3, 20 from dual union all
12 select 'b', 3, 4 from dual union all
13 select 'c', 3, 12 from dual
14 )
15 select gbc,
16 sum(case when id = 1 then value end) as abc,
17 sum(case when id = 2 then value end) as def,
18 sum(case when id = 3 then value end) as ghi,
19 sum(case when id = 4 then value end) as jkl
20 from temp
21 group by gbc;
GBC ABC DEF GHI JKL
--- ---------- ---------- ---------- ----------
a 50 30 20
b 15 10 4
c 34 30 12
SQL>
或者,如果我理解您的查询,您只需将其用作 CTE 并提取所需的值:
with temp as
(select
typeid,
objectid
,trunc(datetime + 1 / (24 * 12), 'HH24') datetime
,timezone
,value as value
,loadid loadid
from prod.ts_wsi_hourly_observation
where typeid in(1,2,3,4)
)
select sum(case when typeid = 1 then value end) as abc,
sum(case when typeid = 2 then value end) as def,
sum(case when typeid = 3 then value end) as ghi,
sum(case when typeid = 4 then value end) as jkl
from temp
group by objectid;