?
Solved

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

Posted on 2014-02-19
5
Medium Priority
?
466 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 20

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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

864 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