我想使用 oracle sql 查询显示如下输出
输入:
name col1 col2
A 11 12
B 12 10
输出:
name col1 col2 tot
A 11 12 23
B 10 10 20
tot 21 22 43
尝试:
select coalesce(name, 'Tot') name,
sum(col1) col1,
sum(col2) col2,
sum(col1+col2) tot
from myTable
group by rollup(name)
SQLFiddle在这里。
您要求的内容可以直接使用group by with rollup
:
select coalesce(name, 'tot'), sum(col1) as col1, sum(col2) as col2, sum(col1+col2) as total
from t
group by name with rollup
order by (case when name is null then 1 else 0 end), name