SQL查询选择求和点

时间:2019-05-30 15:01:25

标签: sql-server

我有2张桌子XemDiem

BieuDo

我想编写选择分数的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

结果应如下所示 enter image description here

1 个答案:

答案 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