이와 비슷한 것 ... 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을 직접 작성하는 것이 좋습니다. –
피벗을 사용하는 경우 동일한 열에 MAX 및 SUM이라는 두 개의 절을 어떻게 쓰겠습니까? – user3359574