Solved

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

Posted on 2013-12-11
5
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 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