문제

I have the following query that will return the three following results:

Name of all students who attended events Name of all faculty members who attended events name of all firm contacts who attended events

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

I would like to sort this first by Student_name, Faculty_name, and then by Firm_Contact_Name, in that order. However, when I enter the following at the bottom of my query

ORDER BY Student_Name, Faculty_Name, Firm_Contact_Name

I receive this error

"The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."

Any thoughts?

Thanks

도움이 되었습니까?

해결책

This should work:

SELECT *
FROM (<your current query here>) AS X
ORDER BY Student_Name, Faculty_Name, Firm_Contact_Name
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top