您沒有指定要使用甲骨文的版本。如果你在Oracle 11g中,那麼你可以使用PIVOT
函數。如果不是,那麼您可以使用CASE
語句的聚合函數。下面是如何產生的結果severl版本:
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
:
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