只是想知道我的SQL语句有什么问题

时间:2015-02-23 08:02:10

标签: group-by

我无法使用此代码正确地对CourseCode列进行分组,您能否帮助我如何对CourseCode和SUM进行分组,以确定在特定月份有多少BSIT和ICT借书。

use CLS

Select C.CourseCode,
Case When DATENAME(MONTH, T.DateBorrowed) = 'January' then SUM(C.CourseCode) else 0 end as January,
Case When DATENAME(MONTH, T.DateBorrowed) = 'February' then SUM(C.CourseCode) else 0 end as February,
Case When DATENAME(MONTH, T.DateBorrowed) = 'March' then SUM(C.CourseCode) else 0 end as March
From TBL_Transactions T left outer join (TBL_Member M left outer join TBL_Course C ON M.CourseID = C.CourseID) ON T.MemberIDNo = M.MemberIDNo
Group by C.CourseCode, T.DateBorrowed!

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个......

SELECT C.CourseCode, DATENAME(MONTH, T.DateBorrowed), SUM(C.CourseCode)
FROM TBL_Transactions T LEFT OUTER JOIN 
(TBL_Member M LEFT OUTER JOIN TBL_Course C ON M.CourseID = C.CourseID) 
ON T.MemberIDNo = M.MemberIDNo 
GROUP BY C.CourseCode, DATENAME(MONTH, T.DateBorrowed)!