Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-12-11
5
Medium Priority
?
480 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 1000 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 1000 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 66

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 70

Expert Comment

by:Scott Pletcher
ID: 39714620
The CROSS APPLY method shown by IMSUKH should perform the best, by far.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

885 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