Solved

SQL Select Command to produce unique value list for all table columns

Posted on 2013-12-11
5
452 Views
Last Modified: 2014-01-11
I need a MS SQL Select Command that will produce a unique set of values from multiple columns.  This SQL Select command needs to read through all 25 columns of data  and provide a list of only the unique values.    For example:

TableA:

Col1        Col2        Col3       Col25
------       --------      --------     -------
Sugar     Honey     Wheat    Milk
Wheat   Sugar       Corn       Salt
Flour     Corn         Honey

Expected Result would be only the unqiue values from all columns:

Result
--------
Sugar
Wheat
Honey
Milk
Salt
Corn
Flour
0
Comment
Question by:hojohappy
5 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39713152
You're going to want to unpivot the data then group it. Sorry I don't have time to do that now, but there is the unpivot example which hopefully you can translate into your situation.

http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
ID: 39713292
SELECT DISTINCT Result
FROM
(
   SELECT DISTINCT Col1 AS Result FROM TableA
   UNION
   SELECT DISTINCT Col2 FROM TableA
   UNION
   SELECT DISTINCT Col3 FROM TableA
   UNION
   ...
)
0
 

Assisted Solution

by:IMSUKH
IMSUKH earned 250 total points
ID: 39713418
SELECT distinct PPID
FROM colmerg
CROSS APPLY (VALUES (col1),
                    (col2),
                    (col3),----(col24) ) AS Lines(PPID)
ORDER BY PPID


pls check this
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39714064
Guessing the above comments answered your question.

What is the logic for the sorting of your expected return set?
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39714620
The CROSS APPLY method shown by IMSUKH should perform the best, by far.
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

Suggested Solutions

Title # Comments Views Activity
push and Pull replication 31 45
query output (script) from a stored procedure 4 33
SQL QUERY 3 33
query analyser in sql server 2016 express edition 2 24
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

680 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