SQL:每年计算电子邮件数

时间:2014-01-13 08:31:45

标签: sql sql-server

我想计算每年在我的数据库中发送的邮件数量。

我试过

SELECT Distinct year(RCM_DateSent) FROM Mail
ORDER BY RCM_DateSent

另外

SELECT COUNT(*)AS [Send], RCM_DateSent FROM Mail
GROUP BY RCM_DateSent 
ORDER BY [Send]

结果不是我想要的,我想知道每年可以发送电子邮件。

2 个答案:

答案 0 :(得分:6)

select year(rcm_dateSent) as yearSent, count(*) as numbersend
from mail
group by year(rcm_dateSent)
order by yearSent

答案 1 :(得分:1)

你必须'按年分组'。像这样:

SELECT Year(RCM_DateSent) AS YearOfMail, count(*) AS TotalMailsSent  
FROM Mail  
GROUP BY Year(RCM_DateSent)  
ORDER BY Year(RCM_DateSent)

PS:最好在分组中使用年份(RCM_DateSent)和& order by,而不是别名。