You can't include Count(*) in the GROUP BY clause.
Birthdate should not be in the SELECT statement: you won't get any kind of
summary if you have it. As well, what's Contacts.ICSNumberID? If it's unique
for each person, you can't include it if you want a summary.
The formula I showed in my example for calculating the age should not be put
in the Count function: it should replace your formula (as
(Date()-([Birthdate]))/365.25 isn't a particularly accurate way to calculate
age)
All you need is
SELECT
DateDiff("yyyy",[Birthdate],Date())-IIf(Format(Date(),"mmdd")<Format([Birthdate],"mmdd"),1,0)
AS Age, Count(*)
FROM Contacts
GROUP BY
DateDiff("yyyy",[Birthdate],Date())-IIf(Format(Date(),"mmdd")<Format([Birthdate],"mmdd"),1,0)
HAVING (((Contacts.Active)="Y"));
If ICSNumberID makes sense to have in the summary, then you'd have
SELECT ICSNumberID,
DateDiff("yyyy",[Birthdate],Date())-IIf(Format(Date(),"mmdd")<Format([Birthdate],"mmdd"),1,0)
AS Age, Count(*)
FROM Contacts
GROUP BY ICSNumberID,
DateDiff("yyyy",[Birthdate],Date())-IIf(Format(Date(),"mmdd")<Format([Birthdate],"mmdd"),1,0)
HAVING (((Contacts.Active)="Y"));
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Andre C said:
The SQL for your query should look like:
SELECT DateDiff("yyyy", [DOB], Date()) - IIf(Format(Date(), "mmdd") <
Format( [DOB], "mmdd"), 1, 0) AS Age, Count(*)
FROM MyTable
GROUP BY DateDiff("yyyy", [DOB], Date()) - IIf(Format(Date(), "mmdd") <
Format( [DOB], "mmdd"), 1, 0)
I tried to convert this thus
SELECT Contacts.Birthdate, Contacts.Active, Contacts.ICSNumberID,
(Date()-([Birthdate]))/365.25 AS Age,
Count(DateDiff("yyyy",[Birthdate],Date())-IIf(Format(Date(),"mmdd")<Format([Birthdate],"mmdd"),1,0))
AS Age2, Count(*)
FROM Contacts
GROUP BY Contacts.Birthdate, Contacts.Active, Contacts.ICSNumberID,
(Date()-([Birthdate]))/365.25, Count(*)
HAVING (((Contacts.Active)="Y"));
but it complained that "cannot have aggrgated funcitonin group by
count(*)