为什么这个查询没有输出? |我想过滤掉那些从未获得最高或最低分数的('student_id',student_name')

问题描述 投票:0回答:1

查询1:

WITH cte AS (
    SELECT 
        *,
        MAX(score) OVER (PARTITION BY exam_id) AS max_score,
        MIN(score) OVER (PARTITION BY exam_id) AS min_score
    FROM
        student JOIN exam
        USING (student_id)
    ORDER BY
            exam_id, student_id
) SELECT
        *
    FROM cte;

通过查询1,你可以检查Jade是不是得分最高或最低的人。

查询2:

WITH cte AS (
    SELECT 
        *,
        MAX(score) OVER (PARTITION BY exam_id) AS max_score,
        MIN(score) OVER (PARTITION BY exam_id) AS min_score
    FROM
        student JOIN exam
        USING (student_id)
    ORDER BY
            exam_id, student_id
) SELECT
        DISTINCT student_id
    FROM cte
    WHERE
        score = max_score OR score = min_score;

通过查询2,您可以检查Jade的'student_id'是否缺失

问题3:

WITH cte AS (
    SELECT 
        *,
        MAX(score) OVER (PARTITION BY exam_id) AS max_score,
        MIN(score) OVER (PARTITION BY exam_id) AS min_score
    FROM
        student JOIN exam
        USING (student_id)
    ORDER BY
            exam_id, student_id
) SELECT
    DISTINCT student_id, student_name
    FROM cte
    WHERE
        student_id NOT IN ( SELECT DISTINCT student_id
                            FROM cte
                            WHERE score = max_score OR score = min_score );


那么,为什么查询3没有输出呢?但是当我硬编码从查询 2 中获得的“student_id”值时。然后它会产生正确的结果,我认为列表中有“Jade”。


原始数据:

CREATE TABLE IF NOT EXISTS Student (student_id INT, student_name VARCHAR(30));
CREATE TABLE IF NOT EXISTS Exam (exam_id INT, student_id INT, score INT);

INSERT INTO Student (student_id, student_name) VALUES (1, 'Daniel');
INSERT INTO Student (student_id, student_name) VALUES (2, 'Jade');
INSERT INTO Student (student_id, student_name) VALUES (3, 'Stella');
INSERT INTO Student (student_id, student_name) VALUES (4, 'Jonathan');
INSERT INTO Student (student_id, student_name) VALUES (5, 'Will');

INSERT INTO Exam (exam_id, student_id, score) VALUES (10, 1, 70);
INSERT INTO Exam (exam_id, student_id, score) VALUES (10, 2, 80);
INSERT INTO Exam (exam_id, student_id, score) VALUES (10, 3, 90);
INSERT INTO Exam (exam_id, student_id, score) VALUES (20, 1, 80);
INSERT INTO Exam (exam_id, student_id, score) VALUES (30, 1, 70);
INSERT INTO Exam (exam_id, student_id, score) VALUES (30, 3, 80);
INSERT INTO Exam (exam_id, student_id, score) VALUES (30, 4, 90);
INSERT INTO Exam (exam_id, student_id, score) VALUES (40, 1, 60);
INSERT INTO Exam (exam_id, student_id, score) VALUES (40, 2, 70);
INSERT INTO Exam (exam_id, student_id, score) VALUES (40, 4, 80);

向我解释为什么会发生这种情况,或者如果您想向我展示更好的方法,那么这是非常可观的。

sql mysql join subquery common-table-expression
1个回答
0
投票

我在第二个子查询中重新排列了您的查询,使其更有条理,现在似乎工作正常。

WITH cte AS (
    SELECT 
        *,
        MAX(score) OVER (PARTITION BY exam_id) AS max_score,
        MIN(score) OVER (PARTITION BY exam_id) AS min_score
    FROM
        student JOIN exam
        USING (student_id)
    ORDER BY
            exam_id, student_id
),
cte2 as (
  SELECT
        DISTINCT student_id
    FROM cte
    WHERE
        score = max_score OR score = min_score 
)
  
  SELECT
       *
    FROM cte
    WHERE
        student_id not in (select student_id from cte2);

小提琴示例

© www.soinside.com 2019 - 2024. All rights reserved.