Select SELECT 案例中的其他数据

Select Other Data in SELECT CASE

查询 return StudentId 和 HaveGift,现在我想在 (studentId) returned 时使用另一个 select 从 tbl_Students (StuName,StuLName) 中查找学生信息,...)。 SQL return 有礼物编号:

CREATE PROCEDURE SelectGiftsTest
    @StudentId              INT,
    @DateMinCur     NVARCHAR(12),
    @DateMaxCur     NVARCHAR(12),
    @DateMinPrev    NVARCHAR(12),
    @DateMaxPrev    NVARCHAR(12)

AS
    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>=@DateMinPrev AND Date <= @DateMaxPrev   AND StudentId = @StudentId
      ) 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>=@DateMinCur AND Date <= @DateMaxCur     AND StudentId = @StudentId
      ) as T 
      GROUP BY  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
WHERE Prev.StudentId=@StudentId
RETURN 0

如果我理解正确的话。

使用 Join 根据下一个返回的 studentId 从 tbl_student 获取学生信息:-

而不是:-

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
WHERE Prev.StudentId=@StudentId

类型:

SELECT CASE 
        WHEN(Prev.HighScoreUser <= Cur.HighScoreUser)
        THEN 'Yes'
        ELSE 'No'
        END as HaveGift,Prev.StudentId
        , std.name, std. .... -- put your columns here that refer to student info 

FROM Prev
INNER JOIN Cur
ON Prev.StudentId = Cur.StudentId
INNER JOIN tbl_student  std
    on std.StudentId = Cur.StudentId
WHERE Prev.StudentId=@StudentId

当查看您的代码时,您在 prev 和 cur 中求和 tbl_ActPoint table,但你想要比较 tbl_EvaPoint 差异。

我解释一下:

你比较A+B(第一个并集)和A+C(第二个并集),你只能比较B和C。

我已将您的查询修改为:

with Total as (
    SELECT StudentId, 
    sum(case when Date between @DateMinPrev AND @DateMaxPrev then score else end) ScorePrev, 
    sum(case when Date between @DateMinCur AND @DateMaxCur then score else end) ScoreCur, 
    FROM tbl_ActPoint 
    where StudentId = @StudentId and (Date between @DateMinPrev AND @DateMaxPrev or Date between @DateMinCur AND @DateMaxCur)
    group by StudentId
)
select 
CASE WHEN(ScorePrev <= ScoreCur) THEN 'Yes' ELSE 'No' END as HaveGift, tbl_student.* 
from tbl_student std left outer join total on std.StudentId = Total.StudentId

如果你也想要当前分数,可以这样做:

with Total as (
    SELECT StudentId, 
    sum(case when Date between @DateMinPrev AND @DateMaxPrev then score else end) ScorePrev, 
    sum(case when Date between @DateMinCur AND @DateMaxCur then score else end) ScoreCur, 
    FROM tbl_ActPoint 
    where StudentId = @StudentId and (Date between @DateMinPrev AND @DateMaxPrev or Date between @DateMinCur AND @DateMaxCur)
    group by StudentId
),
CurrentScoreUser as (
    SELECT StudentId, sum(Score) CurrentScore FROM tbl_ActPoint group by StudentId
)

select 
CASE WHEN(ScorePrev <= ScoreCur) THEN 'Yes' ELSE 'No' END as HaveGift, CurrentScoreUser.CurrentScore, 
tbl_student.* 
from tbl_student std left outer join Total on std.StudentId = Total.StudentId
left outer join CurrentScoreUser on CurrentScoreUser.StudentId=tbl_student.StudentId