Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get number of records each month

Status
Not open for further replies.

mrbboy

Technical User
Feb 28, 2007
136
US
I have a table for storing client complaints. The structure of the table is:

tbl_Complaints
ComplaintID <autonumber>
InitiatedBy <text>
DateInitiated <date>
Complaint <Memo>

Every month, I need to make a report on the number of complaints received per month. How do I qeury that from the table?
 
The query is a group by and a count on the field Complaint and add a column in your query where you get the month out of the DateInitiated : format([DateInitiated];"mm")
 
How about:

Code:
SELECT Format([DateInitiated],"yyyy mm") AS MonthInitiated, Count(*) AS Complaints
FROM tbl_Complaints
GROUP BY Format([DateInitiated],"yyyy mm");
 
If you set a date/time parameter [YourReferenceDate], you can give any date and the query will return the number of complaints for the month of that date:

Select Count(*) As NoOfComplaints
From tbl_Complaints
Where Month(DateInitiated) = Month([YourReferenceDate])
And Year(DateInitiated)=Year([YourReferenceDate])

Optionally, you can display the year and month

Select Count(*) As NoOfComplaints, Year(DateInitiated) as ComplaintYear, Month(DateInitiated) as ComplaintMonth
From tbl_Complaints
Where Month(DateInitiated) = Month([YourReferenceDate])
And Year(DateInitiated)=Year([YourReferenceDate])
Group By Year(DateInitiated), Month(DateInitiated)

HTH


[pipe]
Daniel Vlas
Systems Consultant

 
How are ya mrbboy . . .

[blue]danvlas[/blue] has you in tow. I just don't know why [blue]Now() or Date()[/blue] wasn't substituted for [blue]Month([YourReferenceDate]) & Year([YourReferenceDate])[/blue], giving:
Code:
[blue]Where Month(DateInitiated) = Month([purple][b]Now()[/b][/purple])
And Year(DateInitiated)=Year([purple][b]Now()[/b][/purple])[/blue]




Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Remou,

I tried your code but I received the message "Undefined function 'Format'in expression.

AceMan,

I tried the code using Date() in the where condition but that didn't return any records.

SELECT Count(*) AS NoOfComplaints, Year(DateInitiated) AS ComplaintYear, Month(DateInitiated) AS ComplaintMonth
FROM tbl_Complaint
WHERE (((Month([DateInitiated]))=Month(Now())) AND ((Year([DateInitiated]))=Year(Now())))
GROUP BY Year(DateInitiated), Month(DateInitiated);

 
mrbboy . . .

Also, post back the SQL as you have it now! . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
I tried the code using Date() in the where condition but that didn't return any records."

Maybe you don't have any complaints for December 2007...
Good month...

Try the parameter thing, that will let you obtain information for any month and year, not only the current one.

HTH







[pipe]
Daniel Vlas
Systems Consultant

 
danvlas
If format is giving 'undefined function', it is almost certain that there is a problem with Date as well because both are from the same library, yesno?
 
Logically, yes.

However, I haven't suggested Date or Now anywhere. Instead, I suggested using a parameter.

And, strangely enough, the OP has stated:

"I tried the code using Date() in the where condition but that didn't return any records."

which shows that the query was executed after all.

Dan


[pipe]
Daniel Vlas
Systems Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top