一个关于计数的问题,谢谢
原表tb
列1 列2 列3
a 1 r
b 2 r
c 3 ty
求表
列1 列2 列3 总计
a 1 r 3
b 2 r 3
c 3 ty 3
就是把原表加入一个总计列(count(列1))
谢谢!
------解决方案--------------------select *,(select count(1) from tb) from tb
------解决方案--------------------SELECT *,@@rowcount as 总计 from tb
------解决方案--------------------select *,总计 from tb,(select count(1)总计 from tb)t
------解决方案--------------------USE test
GO
-->生成表tb
if object_id('tb') is not null
drop table tb
Go
Create table tb([列1] nvarchar(1),[列2] smallint,[列3] nvarchar(2))
Insert into tb
Select N'a',1,N'r'
Union all Select N'b',2,N'r'
Union all Select N'c',3,N'ty'
select *,COUNT(1)OVER() AS 总计 from tb
Go
/*
列1 列2 列3 总计
---- ------ ---- -----------
a 1 r 3
b 2 r 3
c 3 ty 3
*/
------解决方案--------------------select *,分组合计=(select count(1) from tb tb2 where tb1.列1=tb2.列1) from tb tb1
------解决方案--------------------现学现卖
select *,count(1) over(partition by 列1) AS 分组统计 from tb