Link to home
Start Free TrialLog in
Avatar of hojohappy
hojohappy

asked on

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

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
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

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
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Guessing the above comments answered your question.

What is the logic for the sorting of your expected return set?
The CROSS APPLY method shown by IMSUKH should perform the best, by far.