我有一個表,有4個型別和一個 "值 "列,我需要在一個查詢中顯示每個id的3列。 型別1= abc,型別2= def,型別3= ghi,型別4= jkl 我現在的查詢,目前在一個列中顯示所有的值,我需要每個typeid有一個列,這將是4個不同名稱的列
。SELECT
串列
, 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) 。
| id | 價值 | |
|---|---|---|
| 1 | 50 | 2 |
| 2 | 3 | |
| 3 |
我需要如何顯示這種結果:
| abc | def | GHI | JKL | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 50 | 30 | 30 | 20 | 20 | 85. | 15 | 10 | 44 | 34. | 34 | 30 | 1212 | 8598 |
| 8598 |
有什么辦法可以做到這一點嗎?我需要所有的行,而不是像最大的聚合
。謝謝你
uj5u.com熱心網友回復:我們通常是這樣做的:
我們通常是這樣做的:
SQL> with temp (gbc, id, value) as
2 --樣本資料。
3 (select 'a'/span>, 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 double union all
13 select 'c'/span>, 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,
物件id
,trunc(datetime 1 / (24 * 12)。'HH24') datetime
,時區
,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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325006.html
標籤:
