Select DISTINCT items from multiple columns based upon match in another column

I hope the heading isn't too confusing.

I have a table that consists of 11 fields Field1, Field2, Field3, etc. I would like to have a query that would get the unique items/names from the table based upon a given criteria.

The data in the table consists of an organizational structure, so if a user were to select a supervisor in Field1 his/her subordinates would be listed in Fields2 - Fields11. A supervisor can be on multiple rows and have multiple subordinates.

Field1      Field2      Field3      Field4      Field5

James      Freda      Nancy            
Kirk       Jim      Lonie      Gretta      Marsha
Shelly      Bob      Mark            
James      Bill      Kelly      Todd      Pete


So, if a user wanted all the subordinates for James they would get a list of  Freda, Nancy, Bill, Kelly, Todd, Pete

I hope this makes sense?

This is what I tried but I'm not good at all on SQL statements.

SELECT DISTINCT Field1 from OrgStructure where 'James' IN (Field1) AND (Field2 IS NOTNULL) OR (Field3 IS NOTNULL) OR (Field4 IS NOTNULL) OR (Field5 IS NOTNULL) Order by Field1
LVL 8
ampapaAsked:
Who is Participating?
 
Brendt HessConnect With a Mentor Senior DBACommented:
SELECT Distinct Field2
FROM OrgStructure
WHERE Field1 = 'James'
    AND Field2 Is Not Null

UNION ALL

SELECT Distinct Field3
FROM OrgStructure
WHERE Field1 = 'James'
    AND Field3 Is Not Null

UNION ALL

SELECT Distinct Field4
FROM OrgStructure
WHERE Field1 = 'James'
    AND Field4 Is Not Null

UNION ALL

SELECT Distinct Field5
FROM OrgStructure
WHERE Field1 = 'James'
    AND Field5 Is Not Null
0
 
amit_gCommented:
Try this ...

Select distinct Field2 from OrgStructure where Field1 = 'James'
Union
Select distinct Field3 from OrgStructure where Field1 = 'James'
Union
Select distinct Field4 from OrgStructure where Field1 = 'James'
Union
Select distinct Field5 from OrgStructure where Field1 = 'James'
0
 
curtis591Commented:
select distinct field2 from table where field1='James'
union all
select distinct field3 from table where field1='James'
union all
select distinct field4 from table where field1='James'
union all
select distinct field5 from table where field1='James'
0
 
curtis591Commented:
Looks like we posted that at the same time.
0
 
ampapaAuthor Commented:
Thank's for the extremely fast post as well as at the same time! All 3 answers work except for bhess1 also included the "IS NULL" criteria from my original attempt so I've got to go with his answer for completeness.

Thank's again.
0
All Courses

From novice to tech pro — start learning today.