如何从子查询中获取多行?

时间:2013-10-31 11:04:36

标签: sql sql-server

您好我有以下查询: 在哪里说[Enquete Vraag] = enquete_vraag我尝试将其作为子查询:

[Enquete Vraag] = 
(
 SELECT enquete_vraag
 FROM enquete_vraag
)

因为enquete_vraag中有多行,我收到以下错误:

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

我需要的是显示enquete_vraag中的所有行。

SELECT  d.docent, 
    v.vak,
    [Enquete Vraag] = enquete_vraag,
    [Totaal Aantal Studenten] = 
        (
        SELECT COUNT(student_ID) 
        FROM student 
        LEFT JOIN klas
        ON student.klas_ID = klas.klas_ID
        INNER JOIN kdv
        ON klas.klas_ID = kdv.klas_ID
        INNER JOIN docent
        ON kdv.docent_ID = docent.docent_ID
        INNER JOIN vak
        ON kdv.vak_ID = vak.vak_ID
        WHERE docent.docent_ID = '1158'
        AND vak.vak_ID = '449'
        ),
    [Totaal Aantal Studenten Enquete Ingevuld] = 
        (
        SELECT COUNT(enquete_antwoord_ID) 
        FROM enquete_antwoord
        INNER JOIN kdv
        ON enquete_antwoord.kdv_ID = kdv.kdv_ID
        INNER JOIN vak
        ON kdv.vak_ID = vak.vak_ID
        INNER JOIN docent
        ON kdv.docent_ID = docent.docent_ID
        WHERE docent.docent_ID = '1158'
        AND vak.vak_ID = '449'
        ), 
    [Gemiddelde Antwoord] = 
        (
        SELECT AVG(enquete_antwoord_radio)
        FROM enquete_antwoord
        INNER JOIN KDV
        ON enquete_antwoord.kdv_ID = kdv.kdv_ID
        INNER JOIN vak
        ON kdv.vak_ID = vak.vak_ID
        INNER JOIN docent
        ON kdv.docent_ID = docent.docent_ID
        WHERE docent.docent_ID = '1158'
        AND vak.vak_ID = '449'
        )
    FROM    enquete_vraag ev
    LEFT JOIN enquete_antwoord ea
        ON  ea.enquete_vraag_ID = ev.enquete_vraag_ID
    LEFT JOIN kdv
        ON ea.kdv_ID = kdv.kdv_ID
    INNER JOIN klas k
        ON kdv.klas_ID = k.klas_ID
    INNER JOIN vak v
        ON kdv.vak_ID = v.vak_ID
    INNER JOIN docent d
        ON kdv.docent_ID = d.docent_ID
    LEFT JOIN student s
        ON k.klas_ID = s.klas_ID
   WHERE   d.docent_ID = '1158' 
   AND     v.vak_ID = '449'
   GROUP BY v.vak, d.docent, ev.enquete_vraag; 

通过此查询,我得到以下输出:

docent.........   vak ................. vraag...........studenten......aantal...gemiddeld

Dhr,...........   Linux praktijk....... QUESTION1...... 83............. 2........7

我真正需要的是:

docent.........   vak ................. vraag...........studenten......aantal...gemiddeld

Dhr,............  Linux praktijk....... QUESTION2...... 83.............0.........0

我真的希望有人可以帮助我,整个上午我一直在努力。 感谢

0 个答案:

没有答案