如何从一对中获取单行

时间:2018-04-08 09:11:18

标签: sql-server

enter image description here

这里我只想显示一行。

这是一段有效的低效代码。

Select T1.FacFirstName + ' ' + T1.FacLastName AS FacultyName, T1.FacDept,
T2.FacFirstName + ' ' + T2.FacLastName AS ColleagueName, 
T2.FacDept AS ColleagueDept, 
T1.CourseNo, T1.AVERAGEGRADE AS FACULTYAVGGRADE, 
T2.AVERAGEGRADE AS COLLEAGUEAVGGRADE
FROM    (SELECT FACULTY.FACNO, FACULTY.FACFIRSTNAME, FACULTY.FACLASTNAME, 
        OFFERING.COURSENO, FACULTY.FACDEPT, AVG(ENROLLMENT.ENRGRADE) AS 
        AVERAGEGRADE
        FROM FACULTY, OFFERING, ENROLLMENT 
        WHERE FACULTY.FACNO = OFFERING.FACNO AND ENROLLMENT.OFFERNO = 
        OFFERING.OFFERNO
        GROUP BY ENROLLMENT.OFFERNO, FACULTY.FACNO, FACULTY.FACFIRSTNAME, 
        FACULTY.FACLASTNAME, OFFERING.COURSENO, FACULTY.FACDEPT) T1, 
        (SELECT FACULTY.FACNO, FACULTY.FACFIRSTNAME, FACULTY.FACLASTNAME, 
        OFFERING.COURSENO, FACULTY.FACDEPT, AVG(ENROLLMENT.ENRGRADE) AS 
        AVERAGEGRADE
        FROM FACULTY, OFFERING, ENROLLMENT 
        WHERE FACULTY.FACNO = OFFERING.FACNO AND ENROLLMENT.OFFERNO = 
        OFFERING.OFFERNO
        GROUP BY ENROLLMENT.OFFERNO, FACULTY.FACNO, FACULTY.FACFIRSTNAME, 
        FACULTY.FACLASTNAME, OFFERING.COURSENO, FACULTY.FACDEPT) T2
where T1.CourseNo = T2.CourseNo and T1.FacDept != T2.FacDept

PS:我是SQL和StackOverflow的新手,所以两者的建议都很受欢迎。 TIA。

0 个答案:

没有答案