
February 16th, 2004, 12:28 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Multiple columns with UNION?
I've got a DB with six tables, one for patient details, and five with results from five different (paper) forms. Each form can be filled out more than once and is indexed according to the patient id and the date it was completed. Therefore, one patient could complete a differing number of each forms.
I'm trying to design a query that will list the patient number in one column, and the dates that the forms were completed in five separate columns corresponding to the form type. In other words, for a particular patient there could be 1 record of form A, 5 records of form B, 2 records of form C, etc.. I've run various queries (using grouping), but this creates cascading duplicates; as well as a UNION query, but this puts all the dates into a single column (see below). Is there some way of generating the UNION results into separate columns corresponding to the form type?
SELECT Patients.[Patient ID], AQoL.Date
FROM Patients LEFT JOIN AQoL ON Patients.[Patient ID] = AQoL.[Patient ID];
UNION
SELECT Patients.[Patient ID], HSU.Date
FROM Patients LEFT JOIN HSU ON Patients.[Patient ID] = HSU.[Patient ID];
UNION
SELECT Patients.[Patient ID], KDQOL.Date
FROM Patients LEFT JOIN KDQOL ON Patients.[Patient ID] = KDQOL.[Patient ID];
UNION
SELECT Patients.[Patient ID], HUI.Date
FROM Patients LEFT JOIN HUI ON Patients.[Patient ID] = HUI.[Patient ID];
UNION SELECT Patients.[Patient ID], [Patient Diary].Date
FROM Patients LEFT JOIN [Patient Diary] ON Patients.[Patient ID] = [Patient Diary].[Patient ID];
|