0
您好是SQL Server樞軸查詢其給出類似的輸出:A,B,C,d的SQL Server的樞軸查詢添加低於總
Semester| StudentDesc | [A]| [B] |[C] |[D]
-----------------------------------------
| 2 | Term1 | 20 | NULL| 5 | 10
------------------------------------------
| 3 | Term2 | 10 | 2 | 2 | 1
-----------------------------------------
我將輸出到包括總共(TotalSessions)如:
Semester| StudentDesc | [A]| [B] |[C] |[D] | TotalSessions
---------------------------------------------------------
| 2 | Term1 | 20 | NULL| 5 | 10 | 35
--------------------------------------------------------
| 3 | Term2 | 10 | 2 | 2 | 1 | 15
-------------------------------------------------------
我認爲這將是AS查詢TotalSessions稱爲 計數(Stats.SessionNumber)列
查詢我擁有的是:
SELECT Semester, StudentDesc, [A],[B],[C],[D]
FROM
(
SELECT
Semesters.Semester, Options.StudentDesc,
/*TotalSessions */ Count(Stats.SessionNumber) AS
TotalSessions, TrainerList.ShortName
FROM Semesters, (StudentList_tbl
INNER JOIN
((RegistrarSemestersAndTerms
INNER JOIN
Stats ON (StudentSemestersAndTerms.Semester = Stats.Semester)
AND (StudentSemestersAndTerms.StudentID = Stats.StudentID))
INNER JOIN
Options ON StudentSemestersAndTerms.Q3 = Options.TermID)
ON StudentList_tbl.StudentID = StudentSemestersAndTerms.StudentID)
INNER JOIN
TrainerList ON StudentList_tbl.RTP = TrainerList.TrainerID
GROUP BY Semesters.Semester, Options.StudentDesc, TrainerList.ShortName
) as base_query
PIVOT
(
Sum(TotalSessions) FOR ShortName IN ([A],[B],[C],[D])
) as pivot_query;
感謝
@ 8610 ...它不會爲SUM函數只接受一個在T-SQL參數工作。 – Deepshikha