customername Count_of_order
-------------------------------- -----------------
US 110
UK 103
Total 213有这样一张表,只有前面2条数据,需要用一个SQL对Count_of_order进行汇总,并且要给它指定一个字符串叫total。要查出以上结果集的数据,SQL怎么写啊
-------------------------------- -----------------
US 110
UK 103
Total 213有这样一张表,只有前面2条数据,需要用一个SQL对Count_of_order进行汇总,并且要给它指定一个字符串叫total。要查出以上结果集的数据,SQL怎么写啊
union all
select 'Total',sum(Count_of_order) from tb
union all
select 'total' , sum(Count_of_order) from tb
union all
select 'Total',sum(Count_of_order) from tb
union all
select 'total',sum(Count_of_order) from tb
union all
select 'total' , sum(Count_of_order) from tb同意楼上的.
drop table tb
go
create table tb(
customername nvarchar(25),
Count_of_order int
)
insert into tb
select 'US', 110 union all
select 'UK', 103 select case when grouping(customername)=1 then '合计' else customername end ,sum(Count_of_order) from tb group by customername with cubeUK 103
US 110
合计 213
drop table tb
go
create table tb(
customername nvarchar(25),
Count_of_order int
)
insert into tb
select 'US', 110 union all
select 'UK', 103 Goselect
case when GROUPING(customername)=1
then
'合计'
else
customername
end
Count_of_order,
sum(Count_of_order) as Total
from tb
group by customername,Count_of_order
with rollup
having GROUPING(customername)=1 or GROUPING(Count_of_order)=0
Count_of_order Total
------------------------- -----------
UK 103
US 110
合计 213
if object_id('tb') is not null
drop table tb
go
create table tb(
customername nvarchar(25),
Count_of_order int
)
insert into tb
select 'US', 110 union all
select 'UK', 103 Goselect
case when GROUPING(customername)=1
then
'合计'
else
customername
end
Count_of_order,
sum(Count_of_order) as Total
from tb
group by customername
with rollup
/*the Result*/
(2 row(s) affected)Count_of_order Total
------------------------- -----------
UK 103
US 110
合计 213(3 row(s) affected)
这样不行吗?