【问题标题】:Select Other Data in SELECT CASE在 SELECT CASE 中选择其他数据
【发布时间】:2017-11-12 20:18:26
【问题描述】:

查询返回 StudentId 和 HaveGift,现在我想在 (studentId) 返回时使用另一个选择从 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

【问题讨论】:

  • 您期望 HaveGift = YES?到底是什么问题?
  • 我想根据返回的 studentId 从 tbl_student 中选择学生信息。

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


【解决方案1】:

查看您的代码时,您在 prev 和 cur 中求和 tbl_ActPoint 的分数 表,但您想比较 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

【讨论】:

    【解决方案2】:

    如果我正确理解了您的需求。

    使用Join 根据返回为 next 的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2010-11-24
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多