당신은 당신이 사용하고있는 오라클의 버전을 지정하지 않았습니다. Oracle 11g에 있다면 PIVOT
기능을 사용할 수 있습니다. 그렇지 않은 경우 CASE
문과 함께 집계 함수를 사용할 수 있습니다.
select contents,
sum(case when specialist = 'Har' then update_count else 0 end) Har,
sum(case when specialist = 'Ram' then update_count else 0 end) Ram,
sum(case when specialist in('Har', 'Ram') then update_count else 0 end) Total
from yourtable
group by contents
union all
select 'total',
sum(case when specialist = 'Har' then update_count else 0 end) Har,
sum(case when specialist = 'Ram' then update_count else 0 end) Ram,
sum(case when specialist in('Har', 'Ram') then update_count else 0 end) Total
from yourtable
이 SQL Fiddle with Demo
참조 또는 당신은 GROUP BY ROLLUP
사용할 수 있습니다 : 여기에 결과를 생성하는 방법에 severl 버전은
select
case when contents is null then 'Total' else contents end contents,
sum(case when specialist = 'Har' then update_count else 0 end) Har,
sum(case when specialist = 'Ram' then update_count else 0 end) Ram,
sum(case when specialist in('Har', 'Ram') then update_count else 0 end) Total
from yourtable
GROUP BY rollup(contents);
이 SQL Fiddle with Demo
참조 또는 당신은 함께 PIVOT
을 사용할 수 있습니다 ROLLUP
:
select
case when contents is null then 'Total' else contents end contents,
sum(coalesce(Har, 0)) Har,
sum(coalesce(Ram, 0)) Ram,
sum(coalesce(Har, 0) + coalesce(Ram, 0)) Total
from
(
select specialist, contents, update_count
from yourtable
) src
pivot
(
sum(update_count)
for specialist in ('Har' as Har, 'Ram' as Ram)
) piv
group by rollup(contents)
는 SQL Fiddle with Demo
질문을 편집하여 이미 문제를 해결하려고 시도한 것을 지정해야합니다. – Rcosta
[Oracle SQL 피벗 쿼리] 가능한 중복 (http://stackoverflow.com/questions/4841718/oracle-sql-pivot-query) – APC