Batman
Batman

Reputation: 6353

Group By and Count in Access

I have this table in access and I'm trying to do a Count and a Group by but am getting an error.

enter image description here

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: enter image description here

Upvotes: 1

Views: 21039

Answers (2)

Lord Peter
Lord Peter

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

Zev Spitz
Zev Spitz

Reputation: 15317

Try removing the ; after the WHERE clause. reference

Upvotes: 1

Related Questions