SUM和Count在一个SQL查询中

时间:2017-10-11 02:26:56

标签: sql sql-server count sum

我有这种数据

time                                               Members
-------------------------------------------------- -----------
Jun 23 2016                                           1
Jun 23 2016                                           1
Jun 23 2016                                           2
Jun 29 2016                                           6
Jul 11 2016                                           3
Jul 11 2016                                           1
Jul 13 2016                                           1

我使用此sql查询获取此数据

SELECT CONVERT (VARCHAR(12), a.registered_time), COUNT(b.member_id) AS Members
FROM b
Inner JOIN a ON b.mirror_id = a.mirror_id 
GROUP BY 
(a.registered_time) order by a.registered_time

如果他们的日期相同,我想得到总数的总和,2016年6月23日的日期将有4名成员,依此类推。是否可以在Count()上使用SUM()FUnction?我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

将值转换为date并将其包括在selectgroup by中:

SELECT CONVERT(date, a.registered_time) as dte, COUNT(b.member_id) AS Members
FROM b JOIN
     a
     ON b.mirror_id = a.mirror_id 
GROUP BY CONVERT(date, a.registered_time)
ORDER BY CONVERT(date, a.registered_time);