SQL - 如何避免将三个查询合并为一个

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

我有一个SQL脚本,用于选择已分配学生的作业。为了确定学生是否完成了他的作业,我使用了一个子查询。一旦学生完成作业,他就应该能够完成下一个作业。

我想我可以通过选择尚未完成的前1个作业(0值)来做到这一点。我可以使用第一个查询的附加查询,但随后我需要第三个查询来连接该查询。有没有办法可以实现这个前1任务的选择,其值为0,有2个查询或更少?

第一次尝试

SELECT ag.group_id, 
    ag.title, 
    ac.collection_id, 
    ag.order, 
    ac.NAME, 
    ac.isactive, 
    (SELECT top 1 iscompleted 
        FROM student_completion 
        WHERE fk_collection_id = collection_id 
        AND fk_student_id like '404') 
      AS isCompleted, 
FROM assignments AS ag 
    JOIN assignments_collection AS ac 
    ON ag.fk_collection_id = ac.collection_id 
Order BY group_id

/*
SELECT TOP 1 isCompleted
(SELECT ag.group_id, 
    ag.title, 
    ac.collection_id, 
    ag.order, 
    ac.NAME, 
    ac.isactive, 
    (SELECT top 1 iscompleted 
        FROM   student_completion 
        WHERE  fk_collection_id = collection_id 
            AND fk_student_id like '404') 
    AS isCompleted, 
FROM   assignments AS ag 
    JOIN assignments_collection AS ac 
        ON ag.fk_collection_id = ac.collection_id 
Order BY group_id)
Where isCompleted = 0 

 .........
*/

数据

+----------+--------------+---------------+-------+----------------------+----------+-------------+
| group_id |    title     | collection_id | order |         name         | isactive | isCompleted |
+----------+--------------+---------------+-------+----------------------+----------+-------------+
|        1 | Assingment_1 |             5 |     0 | Welcome to Linux     |        1 |           0 |
|        2 | Assingment_2 |             6 |     0 | Installation         |        1 |           0 |
|        3 | Assingment_3 |             9 |     1 | Intro to Bash        |        1 |           0 |
|        3 | Assingment_4 |             3 |     1 | Intro to Bash part 2 |        1 |           0 |
+----------+--------------+---------------+-------+----------------------+----------+-------------+

预期数据

+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+
| group_id |    title     | collection_id | order |         name         | isactive | isCompleted | available |
+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+
|        1 | Assingment_1 |             5 |     0 | Welcome to Linux     |        1 |           0 |         1 |
|        2 | Assingment_2 |             6 |     0 | Installation         |        1 |           0 |         0 |
|        3 | Assingment_3 |             9 |     1 | Intro to Bash        |        1 |           0 |         0 |
|        3 | Assingment_4 |             3 |     1 | Intro to Bash part 2 |        1 |           0 |         0 |
+----------+--------------+---------------+-------+----------------------+----------+-------------+-----------+

student_completion

+---------------+------------------+-------------+
| FK_studentKey | FK_collectionKey | isCompleted |
+---------------+------------------+-------------+
|           404 |                5 |           1 |
+---------------+------------------+-------------+
sql sql-server tsql join subquery
1个回答
0
投票

你的问题非常缺乏解释。我在这里黑暗中拍摄。这样做你想要的吗?

select d.*
    , available = sc.isCompleted
from Data d
left join student_completion sc on sc.FK_colletionKey = d.collection_id
© www.soinside.com 2019 - 2024. All rights reserved.