我有一个datetime列,即signed_in
列,我需要每隔一周获取数据库中条目的总数,但是下面的代码返回0。请帮助我实现这一点。
SELECT COUNT(*) AS _total
FROM FirstTimer
WHERE MONTH(Signed_in) = DATEPART(WEEK,getdate());
图像链接是sql查询的结果,但是我如何仅获取_total
列
答案 0 :(得分:1)
您似乎想要:
SELECT DATEPART(WEEK, Signed_in) , COUNT(*) AS _total
FROM FirstTimer
WHERE DATEPART(WEEK, signed_in) = DATEPART(WEEK, GETDATE())
GROUP BY DATEPART(WEEK, Signed_in);
编辑:如果只需要_total
,则可以:
SELECT COUNT(*) AS [_total]
FROM FirstTimer
WHERE DATEPART(WEEK, signed_in) = DATEPART(WEEK, GETDATE());
答案 1 :(得分:0)
您可以过滤大于一周中第一天的记录,而不是比较星期数。因为如果您比较周数,还需要比较年份。
SET DATEFIRST 1
SELECT COUNT(*) AS _total
FROM FirstTimer
WHERE
Signed_in > SELECT CAST( DATEADD( DAY, 1 - DATEPART(WEEKDAY,GETDATE()) ,GETDATE()) AS DATE )