我需要在表格底部添加一个总计。这在我的场景中可能吗?
select country, count(*) from customer
group by country
Country Count
USA 5
UK 10
Canada 15
Russia 25
55 (Requested from SO community)
最佳答案
使用rollup()
select country, count(*)
from customer
group by rollup (country )
如果你还想要标签“Total”,你可以使用grouping
函数:
select case
when grouping(country) = 1 then 'Total'
else country
end as country,
count(*)
from customer
group by rollup (country )
关于sql - 在底部添加总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601153/