在 SELECT CASE SQL 中分组
Group BY in SELECT CASE SQL
嗨,我有一个查询 return YES or NOT for each user now in result 我想添加 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
很简单。只需添加列:
SELECT CASE
WHEN(Prev.HighScoreUser <= Cur.HighScoreUser)
THEN 'Yes'
ELSE 'No'
END as HaveGift,
Prev.StudentId
FROM Prev
INNER JOIN Cur
ON Prev.StudentId = Cur.StudentId
嗨,我有一个查询 return YES or NOT for each user now in result 我想添加 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
很简单。只需添加列:
SELECT CASE
WHEN(Prev.HighScoreUser <= Cur.HighScoreUser)
THEN 'Yes'
ELSE 'No'
END as HaveGift,
Prev.StudentId
FROM Prev
INNER JOIN Cur
ON Prev.StudentId = Cur.StudentId