在SELECT CASE中选择其他数据

时间:2017-06-11 06:39:02

标签: sql sql-server select stored-procedures select-case

查询返回StudentId和HaveGift,现在我想要当(studentId)返回时我使用另一个select从tbl_Students(StuName,StuLName,...)查找学生信息。 返回HaveGift No的SQL:

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

image

2 个答案:

答案 0 :(得分:1)

如果我理解你需要什么。

使用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

答案 1 :(得分:1)

当你查看代码时,你在tbl_ActPoint中得分为prev和in cur  表,但你想比较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