Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSSQL - Select distinct products that are found in two columns

Suppose I have the following combinations in my dataset:

**ProductA**            **ProductB**
  Apple                   Banana
  Apple                   Orange
  Apple                   Pear
  Banana                  Orange
  Banana                  Pear
  Orange                  Pear

How would I return a complete list of unique products in a single column? Desired output below:

**Products**
Apple
Banana
Orange
Pear

If I do select distinct, I obviously won't get the pear because it's not included in column ProductA.

Any help would be appreciated. Thanks!

like image 601
user1765523 Avatar asked Mar 16 '23 01:03

user1765523


1 Answers

You can UNION them together as a single column:

SELECT  ProductA AS Products
FROM    tablename
UNION
SELECT  ProductB
FROM    tablename;
like image 125
Dave.Gugg Avatar answered Mar 18 '23 16:03

Dave.Gugg