使用SQL组合2个单独的结果集

时间:2011-10-17 11:32:48

标签: sql tsql

我想要在最终结果中合并两个单独的结果集

(I)

SELECT * FROM
    (       
    SELECT T_Student_History.StudenID,
    Convert(varchar(11), MAX(T_Student_History.Created),101)AS 'Student Last entry',
    'History' AS 'Subject Name' 
    FROM T_Student_History 
    Group BY T_Student_History.StudentID
    UNION ALL

    SELECT T_Student_geography.StudentID,
    Convert(varchar(11), MAX(T_Student_geography.Created),101)AS 'Student Last entry',
    'Geography' AS 'Subject Name' 
    FROM T_Student_geography
    Group BY T_Student_geography.StudentID
    Union ALL

    SELECT T_Student_Computing.StudentID,
    Convert(varchar(11), MAX(T_Student_Computing.Created),101)AS 'Student Last entry',
    'Computing' AS 'Subject Name' 
    FROM  T_Student_Computing
    Group BY  T_Student_Computing.StudentID
) AS T_AllSubjects

(II)

 SELECT  T_Students.Forename+' '+T_Students.Surname AS 'Student Description',
    T_Students.Email,
    T_Students.Mobile,

    FROM T_Students

我想要一个结果:

forename  surname  mobile  StudentID  Student Last Entry  Subject Name
--------  -------  ------  ---------  ------------------  ------------

2 个答案:

答案 0 :(得分:2)

如果我正确理解你,我认为你想在这里使用WITH,如:

WITH T_Subjects AS
( 
SELECT T_Student_History.StudenID, 
    Convert(varchar(11), MAX(T_Student_History.Created),101)AS 'Student Last entry', 
    'History' AS 'Subject Name'  
    FROM T_Student_History  
    Group BY T_Student_History.StudentID 
    UNION ALL 

    SELECT T_Student_geography.StudentID, 
    Convert(varchar(11), MAX(T_Student_geography.Created),101)AS 'Student Last entry', 
    'Geography' AS 'Subject Name'  
    FROM T_Student_geography 
    Group BY T_Student_geography.StudentID 
    Union ALL 

    SELECT T_Student_Computing.StudentID, 
    Convert(varchar(11), MAX(T_Student_Computing.Created),101)AS 'Student Last entry', 
    'Computing' AS 'Subject Name'  
    FROM  T_Student_Computing 
    Group BY  T_Student_Computing.StudentID 
)

然后就这样做:

  SELECT  
  T_Students.StudentID
  T_Students.Forename+' '+T_Students.Surname AS 'Student Description',                  
  T_Students.Email,
  T_Students.Mobile,
  T_Subjects.StudentLastEntry 
  T_Subjects.Subject Name
FROM T_Students 
LEFT OUTER JOIN T_Subjects ON T_Students.StudentID = T_Subjects.StudentID

答案 1 :(得分:1)

你应该能够像这样内联他们:

SELECT  
  T_Students.StudentID
  T_Students.Forename+' '+T_Students.Surname AS 'Student Description',                  
  T_Students.Email,
  T_Students.Mobile,
  T_AllSubjects.StudentLastEntry 
  T_AllSubjects.Subject Name
FROM T_Students 
INNER JOIN (SELECT * 
            FROM
            (        
             SELECT 
               T_Student_History.StudentID,
               Convert(varchar(11), MAX(T_Student_History.Created),101)AS StudentLastEntry,
               'History' AS SubjectName
             FROM T_Student_History 
             GROUP BY T_Student_History.StudentID
             UNION ALL
             SELECT 
               T_Student_geography.StudentID,
               Convert(varchar(11), MAX(T_Student_geography.Created),101)AS StudentLastEntry,
               'Geography' AS SubjectName
             FROM T_Student_geography
             GROUP BY T_Student_geography.StudentID
             UNION ALL
             SELECT T_Student_Computing.StudentID,
               Convert(varchar(11), MAX(T_Student_Computing.Created),101)AS StudentLastEntry,
               'Computing' AS SubjectName
             FROM  T_Student_Computing
             GROUP BY T_Student_Computing.StudentID
            ) 
           ) T_AllSubjects 
ON T_Students.Studentid = T_AllSubjects.StudentID