34,838
社区成员




create table #T (ccode varchar(4),kind_big_bm varchar(2))
insert into #T
select '0001','01' union all
select '0001','01' union all
select '0001','02' union all
select '0002','02' union all
select '0003','01' union all
select '0003','03'
select ccode,kind_big_bm,hj=count(1) from #t group by ccode,kind_big_bm
union all
select '合计','',count(1) from #t
order by ccode,kind_big_bm
ccode kind_big_bm hj
----- ----------- -----------
0001 01 2
0001 02 1
0002 02 1
0003 01 1
0003 03 1
合计 6
(6 行受影响)
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (ccode varchar(4),kind_big_bm varchar(2))
insert into #T
select '0001','01' union all
select '0001','01' union all
select '0001','02' union all
select '0002','02' union all
select '0003','01' union all
select '0003','03'
select isnull(ccode,'合计') ccode,kind_big_bm,COUNT(*) cnt
from #T
group by ccode,kind_big_bm
with rollup
having GROUPING(ccode)=1 or GROUPING(kind_big_bm)=0
/*
/*
ccode kind_big_bm cnt
----- ----------- -----------
0001 01 2
0003 01 1
0001 02 1
0002 02 1
0003 03 1
合计 6
(6 行受影响)
*/
*/
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (ccode varchar(4),kind_big_bm varchar(2))
insert into #T
select '0001','01' union all
select '0001','01' union all
select '0001','02' union all
select '0002','02' union all
select '0003','01' union all
select '0003','03'
select ccode,kind_big_bm,COUNT(*) cnt
from #T
group by ccode,kind_big_bm
union all
select N'合计' as ccode,'',COUNT(*) cnt
from #T
/*
ccode kind_big_bm cnt
----- ----------- -----------
0001 01 2
0003 01 1
0001 02 1
0002 02 1
0003 03 1
合计 6
(6 行受影响)
*/
select ccode,kind_big_bm,hj=count(1) from tba group by ccode,kind_big_bm
union all
select '合计','',count(1) from tba