Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union query or combining records? 1

Status
Not open for further replies.

tyemm

Technical User
Feb 28, 2005
66
US
I need help combining records to create a new table with more fields. Here's a conceptual example: I have one table with three fields:
Name Dept Facility
Joe A 1
Joe A 2
Steve B 1
Bill A 2

As there's only one Joe here, my goal is a table/query:
Name Facility(1)Dept Facility(2)Dept
Joe A A
Steve B
Bill A

etc. That is, one record per name, including everyone with at least one Facility/Dept combination, but permitting one name to have two of these.

I wish I could express this more efficiently!

Thanks,
Tom
 
A starting point:
SELECT U.Name, Max(U.Dept1) AS [Facility(1)Dept], Max(U.Dept2) AS [Facility(2)Dept]
FROM (
SELECT Name, Dept AS Dept1, ' ' AS Dept2 FROM theTable WHERE Facility=1
UNION ALL SELECT Name, ' ', Dept FROM theTable WHERE Facility=2
) AS U
GROUP BY U.Name

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top