我怎样才能获得每周的行数?

时间:2017-12-15 14:19:59

标签: sql-server tsql

我从表中选择:

SELECT DATEPART(WEEK, DayTime) as WeekNumber FROM dbo.Transactions

结果:

WeekNumber
1
1
1
2
2
3
4
4
5
4
5
3

如何计算每周的行数?

我需要这样的东西:

WeekNumber    CountOfRows
1             3
2             2
3             2
4             3
5             1

2 个答案:

答案 0 :(得分:3)

您需要GROUP BY周编号并使用COUNT功能

SELECT DATEPART(WEEK, DayTime), COUNT(*) 
FROM dbo.Transactions
GROUP BY DATEPART(WEEK, DayTime)

答案 1 :(得分:0)

SELECT DATEPART(WEEK, DayTime) as WeekNumber, COUNT(1) as CountOfRows  
FROM dbo.Transactions 
GROUP BY WeekNumber;