2016-08-11 5 views
0

私は以下のようなテーブルを持っています。週 ストレージ内のすべてのダウンロードの合計 - - 週SQL:データをクエリするピボットの方法

Customer Download Upload Storage 
    A   4  4  110 
    B   2  2  200 

における最高値はどのように私は

ダウンロード問い合わせると列

Customer MetricName MetricValue Date 
A   Upload 2   10-AUG-2007 
A   Download 2   10-AUG-2007 
A   Storage 100  10-AUG-2007 
A   Storage 110  11-AUG-2007 
B   Storage 200  11-AUG-2007 
A   Upload 2   12-AUG-2007 
A   Download 2   12-AUG-2007 
B   Upload 2   10-AUG-2007 
B   Download 2   10-AUG-2007 

使い方先週のようにショーの行データにしたいですピボットやその他の方法を使用してこれを達成する

+2

何を試しましたか?共通のテーブル式(またはネストされた選択)を使用して、必要な行をフィルタリングし、PIVOTを使用することができます – Matt

+0

'Download'、' Upload'または 'Storage'の3つのオプションだけですか? - そうなら、SQLを直接書くほうがよいでしょう。 –

+0

ピボットを使用すると、同じ列にMAXとSUMという2つの句をどのようにしますか? – user3359574

答えて

0

このようなもの... 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 
+0

これは私が探していたものでした。ありがとう!!! – user3359574

関連する問題