SQL select语句嵌套子查询

时间:2014-07-01 18:53:48

标签: sql select subquery

我在select sql语句中围绕嵌套子查询缠绕我的大脑时遇到问题。基本上我在这里工作得很好,但肯定有一种方法可以让这个查询比下面的当前设置更加动态和优雅:

    SELECT Jobs.JobID, Jobs.JobName, 
    (

            SELECT COALESCE(SUM(JobQualifiers.Weight), 0)   
            FROM Jobs j1
            INNER JOIN 
            JobQualifiers ON j1.JobID = JobQualifiers.JobID 
            INNER JOIN  
            Qualifications ON Qualifications.QualificationID = JobQualifiers.QualificationID
            INNER JOIN
            SubGroupQualifiers sgq ON Qualifications.QualificationID = sgq.QualificationID
            INNER JOIN
            QualificationSubGroups ON QualificationSubGroups.QualificationSubGroupID = sgq.SubGroupID
        WHERE 
            j1.JobID = Jobs.JobID
            AND
            QualificationSubGroups.QualificationSubGroupID = 6

    ) as SubGroup6,
    (

            SELECT 
            COALESCE(SUM(JobQualifiers.Weight), 0)
            FROM Jobs j2
            INNER JOIN 
            JobQualifiers ON j2.JobID = JobQualifiers.JobID 
            INNER JOIN  
            Qualifications ON Qualifications.QualificationID = JobQualifiers.QualificationID
            INNER JOIN
            SubGroupQualifiers sgq ON Qualifications.QualificationID = sgq.QualificationID
            INNER JOIN
            QualificationSubGroups ON QualificationSubGroups.QualificationSubGroupID = sgq.SubGroupID
        WHERE 
            j2.JobID = Jobs.JobID
            AND
            QualificationSubGroups.QualificationSubGroupID = 7

    ) as SubGroup7,
(

        SELECT COALESCE(SUM(JobQualifiers.Weight), 0)   
        FROM Jobs j3
        INNER JOIN 
        JobQualifiers ON j3.JobID = JobQualifiers.JobID 
        INNER JOIN  
        Qualifications ON Qualifications.QualificationID = JobQualifiers.QualificationID
        INNER JOIN
        SubGroupQualifiers sgq ON Qualifications.QualificationID = sgq.QualificationID
        INNER JOIN
        QualificationSubGroups ON QualificationSubGroups.QualificationSubGroupID = sgq.SubGroupID
    WHERE 
        j3.JobID = Jobs.JobID
        AND
        QualificationSubGroups.QualificationSubGroupID = 8

) as SubGroup8
FROM Jobs

我的目标是使子查询选择动态而不是硬编码到带有Subgroup6,SubGroup7,SubGroup8等的QualificationSubGroups.QualificationSubGroupID。我相信我需要一个嵌套的子查询,但是如何实现它超出了我。这可能是一个非常模糊的描述,所以我很乐意根据需要提供更多细节!感谢任何回复。

2 个答案:

答案 0 :(得分:0)

您可以使用条件聚合,而不是多个子选择,例如:

SELECT    j.JobID
        , j.JobName
        , COALESCE(SUM(CASE WHEN qsg.QualificationSubGroupID = 6 THEN jq.Weight END), 0)
        , COALESCE(SUM(CASE WHEN qsg.QualificationSubGroupID = 7 THEN jq.Weight END), 0)
        , COALESCE(SUM(CASE WHEN qsg.QualificationSubGroupID = 8 THEN jq.Weight END), 0)
FROM Jobs j
INNER JOIN JobQualifiers jq ON j.JobID = jq.JobID
INNER JOIN Qualifications q ON q.QualificationID = jq.QualificationID
INNER JOIN SubGroupQualifiers sgq ON q.QualificationID = sgq.QualificationID
INNER JOIN QualificationSubGroups qsg ON qsg.QualificationSubGroupID = sgq.SubGroupID
WHERE qsg.QualificationSubGroupID IN (6,7,8)
GROUP BY j.JobID
       , j.JobName

答案 1 :(得分:0)

我不确定您要做什么,但您可以使用CASE条件尝试以下

SELECT Jobs.JobID, Jobs.JobName,
CASE WHEN QualificationSubGroups.QualificationSubGroupID = 6
THEN COALESCE(SUM(JobQualifiers.Weight), 0) END as SubGroup6,
CASE WHEN QualificationSubGroups.QualificationSubGroupID = 7
THEN COALESCE(SUM(JobQualifiers.Weight), 0) END as SubGroup7,
CASE WHEN QualificationSubGroups.QualificationSubGroupID = 8
THEN COALESCE(SUM(JobQualifiers.Weight), 0) END as SubGroup8
FROM Jobs j1
INNER JOIN 
JobQualifiers ON j1.JobID = JobQualifiers.JobID 
INNER JOIN  
Qualifications 
ON Qualifications.QualificationID = JobQualifiers.QualificationID
INNER JOIN
SubGroupQualifiers sgq 
ON Qualifications.QualificationID = sgq.QualificationID
INNER JOIN
QualificationSubGroups 
ON QualificationSubGroups.QualificationSubGroupID = sgq.SubGroupID