使用している Oracle のバージョンを指定していません。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 を参照してください
または、次を使用できますGROUP BY ROLLUP
。
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 を参照してください
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 を参照してください