나는 다음과 같은 형식으로 결과 집합을 반환 할 :오라클 - 총 집계 합계 및 피벗 팅 집계 합계를 반환합니까?
YEARMONTH Total ModelA ModelB ModelC
200101 0 0 0 0
200102 10 5 5 0
200103 8 2 2 4
합계가 월 (1 년 기준)로 그룹화 모든 모델 유형에 대한 시간의 합계이며, 개별 모델의 열은 모델 당 시간의 합이다 유형은 1 년 단위로 분류됩니다. 나는 중첩 된 선택 다음과 같은 쿼리를 사용하여 올바른 결과를 얻을 수 있습니다 : 동일한 결과를 달성하기 위해 오라클 (11)의 피벗 기능을 사용하려고
select distinct yearmonth,
sum(a.hours) as Total,
(select sum(b.hours) from model_hours b
where model = 'ModelA' and a.yearmonth = b.yearmonth) as ModelA,
(select sum(b.hours) from model_hours b
where model = 'ModelB' and a.yearmonth = b.yearmonth) as ModelB,
(select sum(b.hours) from model_hours b
where model = 'ModelC' and a.yearmonth = b.yearmonth) as ModelC
from model_hours a
group by yearmonth
order by yearmonth
내가 호기심을하고 총을 제외한 모든 결과를 얻을 수 있어요 다음 쿼리를 사용하여 시간 : 나는 g 또한, 모든 모델에 대한 시간의 합계를 얻는 방법을 알아낼 수 없었다
YEARMONTH ModelA ModelB ModelC
200101 0 0 0
200102 5 5 0
200103 2 2 4
이 결과를 반환
select * from (
select yearmonth, hours, model
from model_hours a
)
pivot
(
sum(hours)
for model in ('ModelA', 'ModelB', 'ModelC')
)
order by yearmonth
이 결과에 연말에 의해 rouped. 가능한가? 그렇다면 중첩 된 선택보다 더 효율적일까요? 이 특정 테이블에는 현재 약 200K 개의 행이 있습니다. forums.oracle.com에서