[英]how to query the count of records from first day to last day of the month
I would like to get the count of every day records from my table. 我想从我的表中获取每天的记录数。 For example I have a table “Employee” with the following fields ID, EmpNo, DateHired. 例如,我有一个带有以下字段ID,EmpNo,DateHired的表“ Employee”。 And I have the following records 而且我有以下记录
ID EmpNo DateHired
1 000001 3/2/2013 12:00:00 AM
2 000002 3/14/2013 12:00:00 AM
3 000003 3/14/2013 12:00:00 AM
4 000004 3/21/2013 12:00:00 AM
5 000005 4/2/2013 12:00:00 AM
6 000006 4/3/2013 12:00:00 AM
7 000007 4/3/2013 12:00:00 AM
8 000008 4/3/2013 12:00:00 AM
9 000009 4/3/2013 12:00:00 AM
10 000010 4/4/2013 12:00:00 AM
11 000011 4/5/2013 12:00:00 AM
12 000012 5/1/2013 12:00:00 AM
And the current month is april, how can I get this value: 而当前月份是4月,如何获得该值:
Count Day
0 4/1/2013 12:00:00 AM
1 4/2/2013 12:00:00 AM
4 4/3/2013 12:00:00 AM
1 4/4/2013 12:00:00 AM
1 4/5/2013 12:00:00 AM
0 4/6/2013 12:00:00 AM
0 4/7/2013 12:00:00 AM
0 4/8/2013 12:00:00 AM
Up to
0 4/30/2013 12:00:00 AM
You need to create a calendar for the whole month of April in order to get the whole dates of the month. 您需要为四月的整个月创建一个日历,以获取该月的整个日期。 With the aid of using Common Table Expression
, you can get what you want. 借助使用Common Table Expression
,您可以获得所需的内容。
After creating a calendar, join it with table Employee
using LEFT JOIN
so dates will have no matches on table Employee
will still be included on the result. 创建日历后,使用LEFT JOIN
将其与表Employee
LEFT JOIN
在一起,因此日期将在表Employee
上不匹配,结果仍将包括在内。
WITH April_Calendar
AS
(
SELECT CAST('20130401' as datetime) AS [date]
UNION ALL
SELECT DATEADD(dd, 1, [date])
FROM April_Calendar
WHERE DATEADD(dd, 1, [date]) <= '20130430'
)
SELECT a.date, COUNT(b.DateHired) totalCount
FROM April_Calendar a
LEFT JOIN Employee b
ON a.date = b.DateHired
GROUP BY a.date
ORDER BY a.date
Try 尝试
SELECT COUNT(*) as 'Count', DateHired as 'Day'
FROM Employee
WHERE DateHired BEWTEEN %date1 AND %date2
GROUP BY DateHired
Untested, should work though. 未经测试,应该可以。
This could be the query... 这可能是查询...
SELECT COUNT(ID) as Count, DateHired FROM Employee GROUP BY DateHired
Following can be helpful in the case... 在这种情况下,以下内容可能会有所帮助...
http://www.sqlite.org/lang_datefunc.html http://www.sqlite.org/lang_datefunc.html
Hope it helps.. 希望能帮助到你..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.