每个日期的 SQL 计数 [英] SQL Count for each date
本文介绍了每个日期的 SQL 计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下需求
我有一个日志表,用于记录每天生成的潜在客户.
I have a logging table which logs som leads generated each day.
现在我需要提取过去 10 天内每天潜在客户数量的报告.
Now I need to pull a report over the amount of leads for each day over the last 10 days.
假设表格如下所示:
tbl_leads
id int,
first_name nvarchar(100),
last_name nvarchar(100),
created_date datetime
我需要计算每天的潜在客户数量,总共 10 天.所以结果集应该是这样的:
And I need to count the number of leads for each day, 10 days total. SO the result set should look something like this:
counted_leads | count_date
5 | 2009-04-30
7 | 2009-04-29
5 | 2009-04-28
7 | 2009-04-27
...等等
有人知道如何以最好的方式做到这一点吗?我目前的解决方案是在 c# 中使用 foreach 进行迭代,但我非常想将它交给 sql server 而不是在 sp 中.
Anyone know how to do this the best possible way? My current solution is iterating with a foreach in c# but I would very much like to hand it on the sql server instead in a sp.
推荐答案
您可以使用:
Select
count(created_date) as counted_leads,
created_date as count_date
from
table
group by
created_date
这篇关于每个日期的 SQL 计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文