아래 내용이 원하시는 결과인지 궁금하네요.
원본 데이터는 기 주신 데이터에 마지막 라인을 포함해서 사용했습니다.
즉,
select 'A' a, '200611' b, 100640 c, '서울' d, 0 e, 0 f, 0 g from dual union all
select 'A', '200611', 100641, '서울', 0, 20, 0 from dual union all
select 'A', '200611', 100642, '부산', 100, 0, 0 from dual union all
select 'A', '200610', 100639, '부산', 100, 100, 100 from dual
이렇게요...
아래 쿼리 참고하시길...
select a.no
,a.a
,a.b
,a.c
,case when a.no=1 then a.d else 'TOT' end d
,sum(a.e) e
,sum(a.e_sum) e_sum
,sum(a.f) f
,sum(a.f_sum) f_sum
,sum(a.g) g
,sum(a.g_sum) g_sum
from (
select case when a.no=1 then a.no else 2 end no
,a.a
,case when a.no=1 then a.b else null end b
,a.c
,case when a.no=1 then a.d else null end d
,case when a.no=1 or a.no=2 then a.e else 0 end e
,case when a.no=1 or a.no=3 then sum(a.e) over (partition by a.no order by a.c) else 0 end e_sum
,case when a.no=1 or a.no=2 then a.f else 0 end f
,case when a.no=1 or a.no=3 then sum(a.f) over (partition by a.no order by a.c) else 0 end f_sum
,case when a.no=1 or a.no=2 then a.g else 0 end g
,case when a.no=1 or a.no=3 then sum(a.g) over (partition by a.no order by a.c) else 0 end g_sum
from (
select b.a no
,case when b.a=1 then a.a else null end a
,case when b.a=1 or b.a=2 then a.b else null end b
,case when b.a=1 then a.c else null end c
,case when b.a=1 then a.d when b.a=2 then 'sub_total' else 'TOT' end d
,sum(a.e) e
,sum(a.f) f
,sum(a.g) g
from (
select 'A' a, '200611' b, 100640 c, '서울' d, 0 e, 0 f, 0 g from dual union all
select 'A', '200611', 100641, '서울', 0, 20, 0 from dual union all
select 'A', '200611', 100642, '부산', 100, 0, 0 from dual union all
select 'A', '200610', 100639, '부산', 100, 100, 100 from dual
) a,
(
select 1 a from dual union all
select 2 from dual union all
select 3 from dual
) b
where a.a = 'A'
and a.b between '200601' and '200611'
group by b.a
,case when b.a=1 then a.a else null end
,case when b.a=1 or b.a=2 then a.b else null end
,case when b.a=1 then a.c else null end
,case when b.a=1 then a.d when b.a=2 then 'sub_total' else 'TOT' end
) a
where a.b='200611'
or a.b is null
) a
group by a.no
,a.a
,a.b
,a.c
,case when a.no=1 then a.d else 'TOT' end