このようなもの... 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
'Download'、' Upload'または 'Storage'の3つのオプションだけですか? - そうなら、SQLを直接書くほうがよいでしょう。 –
ピボットを使用すると、同じ列にMAXとSUMという2つの句をどのようにしますか? – user3359574