Reputation: 6353
I have this table in access and I'm trying to do a Count and a Group by but am getting an error.
What I'm trying to do is group by the source the number of cars which have AC and are made before 2008.
I tried this:
SELECT SOURCE_ID, COUNT (SOURCE_ID) AS VEHICLES_WITH_AC
FROM VEHICLE
WHERE VEH_AC = 'Y'
GROUP BY SOURCE_ID, VEH_AC, VEH_YEAR
HAVING VEH_YEAR <'2008';
But im getting:
Upvotes: 1
Views: 21039
Reputation: 3501
SELECT SOURCE_ID, COUNT (*) AS VEHICLES_WITH_AC
FROM VEHICLE
WHERE VEH_AC = 'Y' AND VEH_YEAR < '2008'
GROUP BY SOURCE_ID;
1) You apparently require totals by source_id and nothing else, so you don't group by anything else.
2) your condition on VEH_YEAR should go in the where clause otherwise all veh_year rows will be selected and thrown away after aggregation.
Upvotes: 3