Access SQL query issue

I am trying to write a simple query to show me my "groups" of PC's I have in an Access database. I have a table called PC_EQUIP with 4 fields that I need: type, model, warranty and serial. This query works fine:

SELECT DISTINCT Type, Model, Warranty FROM PC_EQUIP

This shows me the information I mostly need since we order PC's in groups so the type, model and warranty date of each group will be the same and this query shows me the different groups that I want to see.

I really want to add two more pieces of information. Even though I am using DISTINCT, I would like to see one of the serial numbers for each DISTINCT group, dont care which. Obviously, if I add serial to the query above, I get a list of all records and the "DISTINCT" is useless. I would also like to add a COUNT to the query so it will show me how many rows are rolled up in that DISTINCT clause effectively showing me how many PC's I have on hand for that group,

The serial number part is optional but I cant seem to figure out how to make it return the count for each grouping.

Thanks
Mark LewisAsked:
Who is Participating?
 
PatHartmanConnect With a Mentor Commented:
Jan, That is T-SQL syntax but the tables are Jet/ACE.

Mark, This should do it:

SELECT  Type, Model, Warranty, Max(SerialNumber) as MaxSerial, Count(*) as PCCount
FROM PC_EQUIP
Group By Type, Model, Warranty;
0
 
Jan LouwerensSoftware EngineerCommented:
SELECT DISTINCT Type, Model, Warranty, COUNT(*) OVER (PARTITION BY Type, Model, Warranty) AS Total, FIRST_VALUE(Serial) OVER (PARTITION BY Type, Model, Warranty) AS Serial FROM PC_EQUIP

Open in new window

0
 
Jan LouwerensConnect With a Mentor Software EngineerCommented:
Without analytic functions, you can use this to get the count:

SELECT Type, Model, Warranty, COUNT(*) AS Total FROM PC_EQUIP
GROUP BY Type, Model, Warranty

Open in new window

0
 
Jan LouwerensSoftware EngineerCommented:
Ah, using MIN or MAX on the Serial. So simple. Why didn't I think of that?!
0
 
Mark LewisAuthor Commented:
Just had to change the variable to Serial and it worked perfectly, Thanks. Jan's solution for a query without the inclusion of the serial number also worked great. Thanks again guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.