Solved

How count number of unique text strings exist in a table/field

Posted on 2014-02-19
5
456 Views
Last Modified: 2014-02-19
I have a table named tblDefectCauses that contains an OpenOrdRecID and a field named DefectCauses.

If in the table using job #23904 for example we have the following in the DefectCauses field:

OpenOrdRecID        DefectCause
23904                           Scrap
23904                           Melted
23904                           Scrap
23904                           Burned
23904                           Melted
23904                           Rotten

There are 6 records.  But the are only 4 different Defect causes.  

I want to write a query that will give me a result of 4 (different Defect Causes).

What would the query look like in SQL?
0
Comment
Question by:SteveL13
  • 3
5 Comments
 
LVL 19

Expert Comment

by:Peter Hutchison
ID: 39871916
The following sql command will list all distinct values from a field in a table.
Select Distinct DefectCause from tblDefectCauses;
0
 

Author Comment

by:SteveL13
ID: 39871928
I probably don't understand what you just wrote.  I want to end up with a result of 4 even though there are 6 records.
0
 

Author Comment

by:SteveL13
ID: 39871929
I tried:

SELECT DISTINCT Count(tblCleanedDefects.DefectCause) AS CountOfDefectCause, tblCleanedDefects.OpenOrdRedID
FROM tblCleanedDefects
GROUP BY tblCleanedDefects.OpenOrdRedID
HAVING (((Count(tblCleanedDefects.DefectCause)) Is Not Null))
ORDER BY tblCleanedDefects.OpenOrdRedID;

But I still get 6 instead of 4.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39871937
try this

Select A.OpenOrdRecID,Count(A.DefectCauses) As CountOfDefects
From
(
SELECT D.OpenOrdRecID, D.DefectCauses
FROM tblDefectCauses AS D
GROUP BY D.OpenOrdRecID, D.DefectCauses
) As A
Group By A.OpenOrdRecID

or


Select A.OpenOrdRecID, Count(A.DefectCauses) as CountOfDefects
From
(
SELECT tblDefectCauses.OpenOrdRecID, tblDefectCauses.DefectCauses
FROM tblDefectCauses
GROUP BY tblDefectCauses.OpenOrdRecID, tblDefectCauses.DefectCauses
) As A
Group By A.OpenOrdRecID
0
 

Author Comment

by:SteveL13
ID: 39871962
This worked perfectly.  Thanks.

Select A.OpenOrdRecID, Count(A.DefectCauses) as CountOfDefects
From
(
SELECT tblDefectCauses.OpenOrdRecID, tblDefectCauses.DefectCauses
FROM tblDefectCauses
GROUP BY tblDefectCauses.OpenOrdRecID, tblDefectCauses.DefectCauses
) As A
Group By A.OpenOrdRecID
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question