我想编写选择分数的sql五月,2019年,中期2 这是我从5月选择得分的代码
SELECT
XD.user_Id, XD.[Name],XD.viTri,XD.sex ,SUM(BD.Point) as pointM
FROM
XemDiem XD
INNER JOIN
BieuDo BD On BD.user_Id = XD.user_Id
WHERE Month(BD.[DateTime]) = 5
GROUP BY
XD.user_Id, XD.[Name],XD.viTri ,XD.sex
答案 0 :(得分:0)
尝试以下操作:
select XD.user_Id, XD.[Name],XD.viTri,XD.sex
sum(case when MONTH(convert(datetime,BD.Date,5)) = 5 and Year(convert(datetime,BD.Date,5)) = 2019 then BD.Point else 0 end) as monthP,
sum(case when MONTH(convert(datetime,BD.Date,5)) >= 4 and MONTH(convert(datetime,BD.Date,5)) <= 6 and Year(convert(datetime,BD.Date,5)) = 2019 then BD.Point else 0 end) as trimesterP
sum(case when MONTH(convert(datetime,BD.Date,5)) = 5 and Year(convert(datetime,BD.Date,5)) = 2019 then BD.Point else 0 end) as yearP
from info
XemDiem XD
INNER JOIN
BieuDo BD On BD.user_Id = XD.user_Id
WHERE Month(BD.[DateTime]) = 5
GROUP BY
XD.user_Id, XD.[Name],XD.viTri ,XD.sex