SQl错误:每个GROUP BY表达式必须包含至少一个不是外部引用的列

时间:2016-06-17 03:48:12

标签: sql sql-server

我收到此错误

  

每个GROUP BY表达式必须至少包含一个不是外部引用的列

运行此查询时

SELECT TOP 1 
    SUM(mla.total_current_attribute_value)
FROM 
    partstrack_machine_location_attributes mla (NOLOCK) 
INNER JOIN 
    #tmpInstallParts_Temp installpartdetails ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId 
                                   AND (CASE WHEN mla.machine_side_id IS NULL THEN 1  
             WHEN  mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END
    ) = 1
INNER JOIN 
    partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
INNER JOIN 
    partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id 
INNER JOIN 
    partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id                                                   
WHERE 
    mla.active_ind = 'Y' AND
    ima.ipp_ID IN (SELECT ipp.ipp_id
                   FROM partstrack_individual_physical_part ipp
                   INNER JOIN #tmpInstallParts_Temp tmp ON (ipp.ipp_id = tmp.InstallingPartIPPId OR
               (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1
                  )
GROUP BY 
    ima.ipp_id

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

这是问题第一版的查询文本。 在以后的修订版中,您删除了最后一个右括号),并且查询在语法上变得不正确。您最好检查并修复问题文本并格式化查询文本,以便可读

SELECT TOP 1
    SUM(mla.total_current_attribute_value)
FROM
    partstrack_machine_location_attributes mla (NOLOCK)
    INNER JOIN #tmpInstallParts_Temp installpartdetails 
        ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId 
        AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
            WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END) = 1
    INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
    INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
    INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
    mla.active_ind = 'Y' 
    AND ima.ipp_ID IN 
    (
        Select
            ipp.ipp_id
        FROM
            partstrack_individual_physical_part ipp
            INNER JOIN #tmpInstallParts_Temp tmp 
                ON (ipp.ipp_id = tmp.InstallingPartIPPId 
                OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1)
        GROUP BY
            ima.ipp_id
    )

通过这种格式化,现在很明显有一个带有GROUP BY的子查询。

很可能只是一个错字:你打算写GROUP BY ipp.ipp_id而不是GROUP BY ima.ipp_id

如果您确实希望GROUP BY不在子查询中,而是在主SELECT中,那么您错放了右括号),查询应如下所示:< / p>

SELECT TOP 1
    SUM(mla.total_current_attribute_value)
FROM
    partstrack_machine_location_attributes mla (NOLOCK)
    INNER JOIN #tmpInstallParts_Temp installpartdetails 
        ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId 
        AND (CASE WHEN mla.machine_side_id IS NULL THEN 1
            WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END) = 1
    INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name
    INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id
    INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id
WHERE
    mla.active_ind = 'Y' 
    AND ima.ipp_ID IN 
    (
        Select
            ipp.ipp_id
        FROM
            partstrack_individual_physical_part ipp
            INNER JOIN #tmpInstallParts_Temp tmp 
                ON (ipp.ipp_id = tmp.InstallingPartIPPId 
                OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END) = 1)
    )
GROUP BY
    ima.ipp_id

无论如何,正确格式化源代码确实有帮助。

答案 1 :(得分:0)

Group By ima.ipp_id 

应该适用于外部查询。由于'('它的位置不正确,它适用于内部查询。

现在纠正了查询后,它运行正常,没有任何问题。

最终查询是:

SELECT TOP 1 SUM(mla.total_current_attribute_value) FROM partstrack_machine_location_attributes mla (NOLOCK) INNER JOIN #tmpInstallParts_Temp installpartdetails ON mla.machine_sequence_id = installpartdetails.InstallKitToMachineSequenceId AND (CASE WHEN mla.machine_side_id IS NULL THEN 1 WHEN mla.machine_side_id = installpartdetails.InstallKitToMachineSideId THEN 1 END ) = 1 INNER JOIN partstrack_mes_attribute_mapping mam (NOLOCK) ON mla.mes_attribute = mam.mes_attribute_name INNER JOIN partstrack_attribute_type at (NOLOCK) ON mam.pt_attribute_id = at.pt_attribute_id INNER JOIN partstrack_ipp_mes_attributes ima(NOLOCK) ON at.pt_attribute_id = ima.pt_attribute_id WHERE mla.active_ind = 'Y' AND ima.ipp_ID IN ( Select ipp.ipp_id FROM partstrack_individual_physical_part ipp INNER JOIN #tmpInstallParts_Temp tmp ON (ipp.ipp_id = tmp.InstallingPartIPPId OR (CASE WHEN tmp.InstallingPartIPKId = '-1' THEN 1 END ) =1) ) GROUP BY ima.ipp_id

谢谢大家。