count qry

S

StuJol

using access 2003 i have the following qry

SELECT DateSerial(Year([Date/Time*]),Month([Date/Time*]),Day([Date/Time*]))
AS [Date], [AlarmData Table Filtered by Date].[Event Type], [AlarmData Table
Filtered by Date].Parameter, [AlarmData Table Filtered by Date].Desc2,
Count(*) AS MyCount, [AlarmData Table Filtered by Date].Module
FROM [AlarmData Table Filtered by Date]
GROUP BY
DateSerial(Year([Date/Time*]),Month([Date/Time*]),Day([Date/Time*])),
[AlarmData Table Filtered by Date].[Event Type], [AlarmData Table Filtered by
Date].Parameter, [AlarmData Table Filtered by Date].Desc2, [AlarmData Table
Filtered by Date].Module
HAVING ((([AlarmData Table Filtered by Date].[Event Type])="CHANGE") AND
(([AlarmData Table Filtered by Date].Parameter) Like "*OPSUP") AND
(([AlarmData Table Filtered by Date].Desc2)="NEW VALUE = 1"));

im trying to count number of entries per date but keeps returning 1 and
several instances of same date.
 
J

John Spencer

I've assigned an alias to the table to make this query easier to type. Also
moved the filtering conditions into a Where clause for efficiency and used the
DateValue function to strip off the time instead of using the DateSerial
function with three other functions calls.

SELECT DateValue([Date/Time*]) AS [Date]
, [A].[Event Type]
, [A].Parameter
, [A].Desc2
, Count(*) AS MyCount
, [A].Module
FROM [AlarmData Table Filtered by Date] AS A
WHERE[A].[Event Type]="CHANGE" AND
[A].Parameter Like "*OPSUP" AND
[A].Desc2="NEW VALUE = 1"
GROUP BY DateValue([Date/Time*]),
[A].[Event Type], [A].Parameter, [A].Desc2, [A].Module

If you want a count by date you need to change the query to eliminate the
other fields that you don't want to group by. Perhaps the following -
although you may want to eliminate Module also.

SELECT DateValue([Date/Time*]) AS [Date]
, Count(*) AS MyCount
, [A].Module
FROM [AlarmData Table Filtered by Date] AS A
WHERE[A].[Event Type]="CHANGE" AND
[A].Parameter Like "*OPSUP" AND
[A].Desc2="NEW VALUE = 1"
GROUP BY DateValue([Date/Time*])
, [A].Module

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
D

Daryl S

StuJol -

If you only want the results by date, then remove any fields from the query
that are not limited to one choice (in the WHERE or HAVING clauses), or are
the ones you want to count by (e.g. the date). Any other fields in the GROUP
BY will cause multiple records for each date. In this case, the "[AlarmData
Table Filtered by Date].Module" could contain multiple values, and the
[AlarmData Table Filtered by Date].Parameter could have multiple values. The
criteria is OK, but you don't want to group by or dislplay this last field.

I think you also might want to look into DateValue function instead of the
DateSerial expression you used. You can check out what I used - much simpler.

This is untested, but you get the idea:

SELECT DateValue([Date/Time*])AS [Date],
[AlarmData Table Filtered by Date].[Event Type], [AlarmData Table Filtered
by Date].Desc2,
Count(*) AS MyCount
FROM [AlarmData Table Filtered by Date]
GROUP BY DateValue([Date/Time*]),
[AlarmData Table Filtered by Date].[Event Type], [AlarmData Table Filtered
by Date].Desc2
WHERE ((([AlarmData Table Filtered by Date].[Event Type])="CHANGE") AND
(([AlarmData Table Filtered by Date].Parameter) Like "*OPSUP") AND
(([AlarmData Table Filtered by Date].Desc2)="NEW VALUE = 1"));

If you have issues, post your new SQL and give us an example of the output...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top