我有以下查询将返回以下三个结果:
参加活动的所有学生的姓名 参加活动的所有教员的姓名 参加活动的所有公司联系人的姓名
SELECT S.Last_Name + ', ' + S.First_Name AS Student_Name, '' AS Faculty_Name, '' AS Firm_Contact_Name, E.Event_name, E.Date
FROM dbo.Student AS S INNER JOIN
dbo.Student_Enrollment AS SE ON S.Student_ID = SE.Student_ID INNER JOIN
dbo.Student_Attend AS SA ON SE.Enrollment_ID = SA.Enrollment_ID INNER JOIN
dbo.Event AS E ON SA.Event_ID = E.Event_ID
UNION
SELECT '' AS Student_Name, F.Last_name + ', ' + F.First_name AS Faculty_Name, '' AS Firm_Contact_Name, E.Event_name, E.Date
FROM Faculty F INNER JOIN
Faculty_Attend FA ON F.Fac_ID = FA.Fac_ID INNER JOIN
Event E ON FA.Event_ID = E.Event_ID
UNION
SELECT '' AS Student_Name, '' AS Faculty_name, C.Last_Name + ', ' + C.First_Name AS Firm_Contact_Name, E.Event_name, E.Date
FROM Firm_Contact C INNER JOIN
Firm O ON C.Comp_ID = O.Comp_ID INNER JOIN
Firm_Attend OA ON O.Comp_ID = OA.Comp_ID INNER JOIN
Event E ON OA.Event_ID = E.Event_ID
我想首先按照Student_name,Faculty_name,然后按Firm_Contact_Name按顺序排序。但是,当我在查询底部输入以下内容时
ORDER BY Student_Name, Faculty_Name, Firm_Contact_Name
我收到此错误
" ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP或FOR XML。"
有什么想法吗?
由于
答案 0 :(得分:2)
这应该有效:
SELECT *
FROM (<your current query here>) AS X
ORDER BY Student_Name, Faculty_Name, Firm_Contact_Name