
Signature
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
That will give me all of the records I need, but not in the format I'm
trying to get. Allow me to try to simplify.
Surveys - PK_SurveyID, CQ1, CQ2, CQ3, CQ4
Instructors - PK_InstructorID, FK_SurveyID, IQ1, IQ2, IQ3, IQ4
For every Survey records there can be one or two Instructor records.
If I JOIN the Instructors table twice to Surveys I only get those
Survey records with two Instructors. I need a way to join the tables
with a result structure of:
PK_SurveyID, CQ1, CQ2, CQ3, CQ4, PK_InstructorID, IQ1, IQ2, IQ3, IQ4,
PK_InstructorID, IQ1, IQ2, IQ3, IQ4
and results of:
1, 5, 5, 5, 5, 1, 3, 3, 3, 3, 2, 4, 4, 4, 4
2, 5, 5, 5, 5, 3, 2, 2, 2, 2, NULL, NULL, NULL, NULL, NULL
3, 5, 5, 5, 5, 4, 1, 1, 1, 1, 5, 2, 2, 2, 2
I don't know if this is possible through SQL alone, or if I have to do
some complex reading of one table and then reading of another table
with the results from the first query and then reading the whole
shebang into another array or dataadapter or whatever...
-Sean
ssims - 15 Dec 2006 03:48 GMT
After doing some additional reading on joins and a few hours of trial
and error I came up with a solution to my problem:
SELECT *
FROM Surveys
LEFT OUTER JOIN Instructors I1 ON I1.FK_SurveyID = PK_SurveyID
LEFT OUTER JOIN Instructors I2 ON I1.FK_NameID <> I2.FK_NameID AND
I2.FK_SurveyID = PK_SurveyID
WHERE I1.PK_InstructorID IN (SELECT MIN(PK_InstructorID) FROM
Instructors GROUP BY FK_SurveyID)
-Sean