這樣的事情...... PIVOT
需要相同的聚合函數才能全部應用於col UMNS;舊的「手動」旋轉方式(使用大小寫表達式)更加靈活(如下所示)。我不確定你的意思是「周」 - 我只是把它放在WHERE
條款中。另外,不要使用像DATE這樣的保留字作爲列名(或表名),你不能直接這樣做,而且你不應該用唯一可能的方式來做(使用雙引號) - 這是一個非常糟糕的做法。我將列名Date
更改爲dt
。
with
input_data (customer, metricname, metricvalue, dt) AS (
select 'A', 'Upload' , 2 , to_date('10-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'A', 'Download', 2 , to_date('10-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'A', 'Storage' , 100 , to_date('10-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'A', 'Storage' , 110 , to_date('11-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'B', 'Storage' , 200 , to_date('11-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'A', 'Upload' , 2 , to_date('12-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'A', 'Download', 2 , to_date('12-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'B', 'Upload' , 2 , to_date('10-AUG-2007', 'dd-MON-yyyy') from dual union all
select 'B', 'Download', 2 , to_date('10-AUG-2007', 'dd-MON-yyyy') from dual
)
select customer,
sum(case when metricname = 'Upload' then metricvalue end) as upload,
sum(case when metricname = 'Download' then metricvalue end) as download,
max(case when metricname = 'Storage' then metricvalue end) as storage
from input_data
where dt between to_date('09-AUG-2007', 'dd-MON-yyyy') and
to_date('15-AUG-2007', 'dd-MON-yyyy')
group by customer
order by customer
;
CUSTOMER UPLOAD DOWNLOAD STORAGE
-------- ---------- ---------- ----------
A 4 4 110
B 2 2 200
你試過了什麼?您可以使用公共表格表達式(或嵌套選擇)過濾您想要的數據行,然後使用PIVOT – Matt
這僅僅是3個選項 - 「下載」,「上傳」還是「存儲」? - 如果是這樣,你最好直接寫SQL。 –
如果我使用pivot,我將如何做2個子句 - MAX和SUM在同一列...? – user3359574