2012-11-12 3 views
-7

다음과 같이 oracle에 sql 테이블이 있으며 이름이 범위입니다.테이블 형식으로 출력합니다.

SPECIALIST   CONTENTS UPDATE_COUNT 
Ram     Updates   23 
Har     Legis    6 
Ram     Updates   65 

출력 형식을 아래 형식으로 지정하고 싶습니다.

  Har Ram Total 
Updates 0 88 88 
Legis 6 - 6 
Total 6 88 94 

감사

+3

100 % 진짜 질문을 참조하십시오. gdoron

+0

질문을 편집하여 이미 문제를 해결하려고 시도한 것을 지정해야합니다. – Rcosta

+0

[Oracle SQL 피벗 쿼리] 가능한 중복 (http://stackoverflow.com/questions/4841718/oracle-sql-pivot-query) – APC

답변

3

당신은 당신이 사용하고있는 오라클의 버전을 지정하지 않았습니다. 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

관련 문제