SELECT CASE SQL中的BY组

时间:2017-06-10 15:11:03

标签: sql-server tsql select group-by select-case

嗨我有一个查询,现在每个用户都返回YES或NOT结果我想添加StudentId以显示更好的结果我怎么做?

WITH Prev AS
(
    SELECT StudentId, ISNULL(SUM(Score),0) As HighScoreUser
FROM (SELECT StudentId, Score FROM tbl_ActPoint
UNION ALL
      SELECT StudentId, Score FROM tbl_EvaPoint     WHERE Date>='1396/01/01' and Date <= '1396/01/31'
      ) as T 
      GROUP BY  StudentId
),
Cur AS 
(
    SELECT StudentId, ISNULL(SUM(Score),0) As HighScoreUser
FROM (SELECT StudentId, Score FROM tbl_ActPoint
UNION ALL
      SELECT StudentId, Score FROM tbl_EvaPoint     WHERE Date>'1396/02/01' and Date <= '1396/02/31'
      ) as T 
      GROUP BY  StudentId
)
SELECT CASE 
        WHEN(Prev.HighScoreUser <= Cur.HighScoreUser)
        THEN 'Yes'
        ELSE 'No'
        END as HaveGift
FROM Prev
INNER JOIN Cur
ON Prev.StudentId = Cur.StudentId

enter image description here

1 个答案:

答案 0 :(得分:1)

这很简单。只需添加列:

sender