在UNION之后创建SELECT和JOIN

时间:2013-07-16 09:00:31

标签: sql-server

嗨,我有一个UNION beetween到表格如下:

SELECT GetStudentClassmates.AvatarFileName  as AvatarFileName,
       GetStudentClassmates.UserName        as UserName,
       GetStudentClassmates.RoleId          as RoleId
FROM GetStudentClassmates
WHERE GetStudentClassmates.StudentId = @studentId

UNION

SELECT GetStudentTeachers.AvatarFileName    as AvatarFileName,
       GetStudentTeachers.UserName          as UserName,
       GetStudentTeachers.RoleId            as RoleId
FROM GetStudentTeachers

现在我需要将UNION的结果加入到其他表中,并在最终结果中添加另一列。

这是我正在使用的JOIN代码

LEFT OUTER JOIN Sync_StudentAssignments as StudentAssignments 
  ON @studentId = StudentAssignments.StudentID 
  AND StudentAssignments.Deletion_Date IS NULL
LEFT OUTER JOIN Sync_StudentAssignmentInstances as StudentAssignmentInstances 
  ON StudentAssignments.StudentAssignmentID = StudentAssignmentInstances.StudentAssignmentID 
  AND StudentAssignmentInstances.Deletion_Date IS NULL

我的问题是我不知道在哪里添加选择代码,我觉得这看起来像这样:

SELECT StudentAssignmentInstances.ModuleInstanceId

我的决赛桌应该包含以下列:

AvatarFileName,
UserName,
RoleId,
ModuleInstanceId

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:4)

UNION位括在括号中并将其用作内联表,然后正常加入:

-----------------------------------------------------------
-- choose the exact columns you need here using the aliases
-----------------------------------------------------------
select x.*, ssa.*, ssai.* from
(
   select ....
   union
   select ....
) as x
inner join Sync_StudentAssignments ssa on ...
inner join Sync_StudentAssignmentInstances ssai on ....

答案 1 :(得分:2)

在SQL Server中如果要加入某个联合,可以使用子选择表单或CTE。 在这里,我将使用CTE,因为它在TSQL代码中添加了一些顺序......

;WITH Participants AS (
  SELECT GetStudentClassmates.AvatarFileName  as AvatarFileName,
         GetStudentClassmates.UserName        as UserName,
         GetStudentClassmates.RoleId          as RoleId
  FROM GetStudentClassmates
  WHERE GetStudentClassmates.StudentId = @studentId

  UNION ALL

  SELECT GetStudentTeachers.AvatarFileName    as AvatarFileName,
         GetStudentTeachers.UserName          as UserName,
         GetStudentTeachers.RoleId            as RoleId
  FROM GetStudentTeachers)
SELECT
FROM 
  Participants p
  JOIN WhateverYouWant wyw
    ON wyw.{column1} = p.{column1}
    AND wyw.{column2} = p.{column2}
    ...

此处WhateverYouWant可能是您的Sync_StudentAssignments JOIN Sync_StudentAssignmentInstances。但是,你已经加入了使用变量和const,这将产生笛卡尔积,我相信这不是你的意图。

修改

现在我更仔细地看待你的情况,我相信你必须做到以下几点:

;WITH Participants AS (
  SELECT GetStudentClassmates.AvatarFileName,
         GetStudentClassmates.UserName,
         GetStudentClassmates.RoleId,
         GetStudentClassmates.StudentId       
  FROM GetStudentClassmates
  --WHERE GetStudentClassmates.StudentId = @studentId
  --uncomment previous line only if you need just one student in your recordset

  UNION ALL

  SELECT GetStudentTeachers.AvatarFileName,
         GetStudentTeachers.UserName,
         GetStudentTeachers.RoleId,
         GetStudentClassmates.TeacherId AS StudentId -- just guessing column name
  FROM GetStudentTeachers
  --previous select means you need all of the teachers, no matter which student
)
SELECT
  p.AvatarFileName,
  p.UserName,
  p.RoleId,
  StudentAssignmentInstances.ModuleInstanceId
FROM 
  Participants p
  LEFT JOIN Sync_StudentAssignments as StudentAssignments 
    ON p.StudentId = StudentAssignments.StudentID 
    AND StudentAssignments.Deletion_Date IS NULL
  LEFT JOIN Sync_StudentAssignmentInstances as StudentAssignmentInstances 
    ON StudentAssignments.StudentAssignmentID = StudentAssignmentInstances.StudentAssignmentID 
    AND StudentAssignmentInstances.Deletion_Date IS NULL

答案 2 :(得分:1)

SELECT tbl.AvartarFileName, 
       tbl.UserName, 
       tbl.RoleId, 
       StudentAssignmentInstances.ModuleInstanceId
FROM
(
    SELECT GetStudentClassmates.AvatarFileName  as AvatarFileName,
           GetStudentClassmates.UserName        as UserName,
           GetStudentClassmates.RoleId          as RoleId
      FROM GetStudentClassmates
     WHERE GetStudentClassmates.StudentId = @studentId

     UNION

    SELECT GetStudentTeachers.AvatarFileName    as AvatarFileName,
           GetStudentTeachers.UserName          as UserName,
           GetStudentTeachers.RoleId            as RoleId
           FROM GetStudentTeachers
) AS tbl
LEFT OUTER JOIN Sync_StudentAssignments as StudentAssignments 
  ON @studentId = StudentAssignments.StudentID 
  AND StudentAssignments.Deletion_Date IS NULL
LEFT OUTER JOIN Sync_StudentAssignmentInstances as StudentAssignmentInstances 
  ON StudentAssignments.StudentAssignmentID = StudentAssignmentInstances.StudentAssignmentID 
  AND StudentAssignmentInstances.Deletion_Date IS NULL