我从表中选择:
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
答案 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;