根据日期获取用户数

时间:2014-12-01 10:37:33

标签: sql sql-server sql-server-2008

我有简单的表格,我存储了userID,UserName,Email,RegistrationDate ......

RegistrationDateDateTime数据类型

当我要获取每个日期的用户数时,每行会得到1个,因为每行都有一个唯一的日期时间戳。

SELECT DATEPART(dd, RegistrationDate) AS DD, COUNT(userID)   
FROM USER_Table 
GROUP BY RegistrationDate 
order by RegistrationDate DESC

如果我有100个用户注册,ABove查询会给我100行。

我希望根据日期计算。我尝试了不同的东西但似乎什么都没有用。

3 个答案:

答案 0 :(得分:3)

试试这个:

SELECT Cast(RegistrationDate AS DATE),
       Count(userID)
FROM   User_Table
GROUP  BY Cast(RegistrationDate AS DATE)
ORDER  BY Cast(RegistrationDate AS DATE)DESC 

答案 1 :(得分:1)

您需要按CAST(RegistrationDate as Date)

进行分组
SELECT CAST(RegistrationDate as Date) AS [Date], COUNT(userID)   
FROM USER_Table 
GROUP BY CAST(RegistrationDate as Date)
order by CAST(RegistrationDate as Date) DESC

答案 2 :(得分:0)

SELECT cast(RegistrationDate as Date) AS DD, COUNT(userID)   
FROM USER_Table 
GROUP BY cast(RegistrationDate as Date)
order by cast(RegistrationDate as Date) DESC