Home > Enterprise >  How to count unique items in field in Access query and group them by [feildname]?
How to count unique items in field in Access query and group them by [feildname]?

Time:05-26

[Given Table]

Code Status
AXYZ Success
AXYZ Success
AXYZ Success
AXYZ
AXYZ
BXYZ Success
BXYZ Success
BXYZ Success
BXYZ Success
BXYZ
BXYZ

SQL Command on Access

'''

SELECT [WERKS], count(*) FROM (SELECT DISTINCT Status FROM [Sheet1$]) FROM [Sheet1$] GROUP BY [WERKS]

'''

Expected Output

AXYZ-2

BXYZ-2

Actual Output :

Error in FROM Clause

CodePudding user response:

You can try:

SELECT COUNT(Code), Code FROM tableName WHERE Status not like 'Success' GROUP BY Code

This work for all Status that are not equal to 'Success'.

CodePudding user response:

If you bust it into two groupings you can get the output you want

SELECT t1.Code, Count(t1.Code) AS NumStatus
FROM 
    (SELECT [Sheet1$].Code, [Sheet1$].Status
    FROM [Sheet1$]
    GROUP BY [Sheet1$].Code, [Sheet1$].Status)  AS t1
GROUP BY t1.Code;

Output

CodePudding user response:

try this

SELECT code, count(Distinct code , status) FROM [Sheet1$] GROUP BY code
  • Related