您没有指定要使用甲骨文的版本。如果你在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