Solved

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

Posted on 2013-12-11
5
437 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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:ScottPletcher
Comment Utility
The CROSS APPLY method shown by IMSUKH should perform the best, by far.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now