问题
I would like to retrieve the data between 1 -30 of the current month [ i am using MSACCESS Dbase to do so] Below is the query that i am trying --
SELECT count(usercategory) as category_count ,usercategory FROM user_category
where IssueDate between DATEADD('m', DATEDIFF('m', 0, DATE()) - 0 , 0) and DATEADD('m', DATEDIFF('m', 0, DATE()) + 1, - 1 ) group by usercategory
Data that i am holding in my MSACCESS Dbase -
Category1 9/7/2013 12:00:00 AM
Category1 9/8/2013 12:00:00 AM
Category2 10/8/2013 12:00:00 AM
so output should have only 2 records but my query is giving no results
回答1:
Here is the query I think you need. All the functions it uses are always available in Access SQL regardless of whether the query is run from within an Access session or from without (as in your c# situation).
The db engine will evaluate both those DateSerial
expressions once, then use their results to filter the result set. This approach will be especially fast with an index on IssueDate
.
SELECT
Count(usercategory) AS category_count,
usercategory
FROM user_category
WHERE
IssueDate >= DateSerial(Year(Date()), Month(Date()), 1)
AND IssueDate < DateSerial(Year(Date()), Month(Date()) + 1, 0)
GROUP BY usercategory;
Here is an Access Immediate window session which explains the logic for those DateSerial
expressions ...
? Date()
9/6/2013
? Year(Date())
2013
? Month(Date())
9
' get the date for first of this month ...
? DateSerial(Year(Date()), Month(Date()), 1)
9/1/2013
' now get the date for the last of this month ...
? DateSerial(Year(Date()), Month(Date()) + 1, 0)
9/30/2013
来源:https://stackoverflow.com/questions/18669319/how-to-select-current-dates-from-current-month