SQL Count with Multiple Columns, Single Table

H

hk_powerzone

I have tblInventory with ItemName, ModelNo

Lots of ItemNames and ModelNo's are duplicates so I want to get the following
view:

ItemName, ModelNo, DistinctCount
Ford, F150, 10
Ford, F250, 4
Chevrolet, 1500, 6


So here's the SQL I've been trying but I'm getting an error message:
SELECT Distinct ItemName, ModelNo, Count(Distinct ItemName, ModelNo) as
DistinctMakeModel FROM tblInventory Group By ItemName, ModelNo;

The error says: Syntax Error (missing operator) in query expression 'Count
(Distinct, ItemName, ModelNo)'.

Any ideas?
 
H

hk_powerzone

OK, looks like it was much easier than I expected:

SELECT DISTINCT ItemName, ModelNo, Count(ItemName) AS CountOfItemName
FROM tblInventory
GROUP BY ItemName, ModelNo;
 
B

Bob Barrows

hk_powerzone said:
OK, looks like it was much easier than I expected:

SELECT DISTINCT ItemName, ModelNo, Count(ItemName) AS CountOfItemName
FROM tblInventory
GROUP BY ItemName, ModelNo;

There is no point to including the DISTINCT keyword here. GROUP BY already
guarantees distinct records.

If you are trying to count distinct item names, then you need to use a
subquery:

SELECT ItemName, ModelNo, Count(ItemName) AS CountOfItemName
FROM
(SELECT DISTINCT ItemName, ModelNo, ItemName) As q
GROUP BY ItemName, ModelNo;
 

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