【问题标题】:Column invalid in select list because it is contained in either an aggregate function or the GROUP BY clause选择列表中的列无效,因为它包含在聚合函数或 GROUP BY 子句中
【发布时间】:2018-04-20 13:49:33
【问题描述】:

我知道这个主题已经涵盖了类似的问题,我已经查看了答案,但不幸的是我无法弄清楚如何应用到这个查询。 我知道将 'pe.short_name' 添加到 GROUP BY 也不是处理此问题的最佳方法。 任何建议都非常感谢..

列“pe.short_name”在选择列表中无效,因为它包含 在聚合函数或 GROUP BY 子句中。`

    SELECT CASE 
        WHEN SUBSTRING(s.description, 13, 1) = '.'
            THEN LEFT(s.description, 17)
        ELSE LEFT(s.description, 12)
        END WBSCode
    ,pe.short_name
    ,pn.description ProjectName
    ,(
        SELECT description
        FROM structure
        WHERE structure_code = pe.code346
        ) ActivePortfolio
    ,CASE 
        WHEN ast.Description = 'WAIO'
            THEN anc.Description
        ELSE site.Description
        END SiteFunction
    ,CASE 
        WHEN SUBSTRING(jv.description, 5, 1) = ' '
            THEN LEFT(jv.description, 4)
        WHEN SUBSTRING(jv.description, 7, 1) = ' '
            THEN LEFT(jv.description, 6)
        ELSE jv.Description
        END AS JointVenture
    ,(
        SELECT description
        FROM structure
        WHERE structure_code = pe.code79
        ) AssetClassification
    ,(
        SELECT description
        FROM structure
        WHERE structure_code = pe.code390
        ) InvestmentSize
    ,(
        SELECT RIGHT(import_code, 6)
        FROM structure
        WHERE structure_code = pe.code79
        ) AssetClassCode
    ,CAST(p.period_start AS DATE) Period_Start
    ,SUM(CASE 
            WHEN be.currency_code = 'AUD'
                AND @Currency = 'AUD'
                THEN be.amount
            WHEN be.currency_code = 'USD'
                AND @Currency = 'USD'
                THEN be.amount
            WHEN be.currency_code = 'AUD'
                AND @Currency = 'USD'
                THEN be.amount * ip.pfn_exch_rate_on_date(p.period_start, be.currency_code)
            WHEN be.currency_code = 'USD'
                AND @Currency = 'AUD'
                THEN be.amount * (ip.pfn_exch_rate_on_date(p.period_start, be.currency_code) / ip.pfn_exch_rate_on_date(p.period_start, 'AUD'))
            END) Amount
FROM fm_budget_entry be
JOIN fm_period p ON be.period_id = p.period_id
JOIN fm_budget_line bl ON be.line_id = bl.line_id
    AND bl.version_id = @FinancialVersion
JOIN fm_budget_line_attrib la ON la.line_id = bl.line_id
    AND la.line_attrib_code = 'Wbs138'
JOIN structure s ON la.line_attrib_value = s.structure_code
JOIN fm_account fa ON bl.account_code = fa.account_code
JOIN planning_entity pe ON pe.planning_code = bl.structure_code
JOIN structure ast ON ast.structure_code = pe.code713
    AND ast.Description IN (@Assets)
JOIN structure pn ON pn.structure_code = pe.planning_code
LEFT JOIN structure site ON site.structure_code = pe.code37
LEFT JOIN structure_tree tr ON tr.dsc_code = pe.planning_code
    AND tr.anc_Depth = 5
LEFT JOIN structure anc ON anc.structure_code = tr.anc_code
LEFT JOIN fm_version v ON v.version_id = bl.version_id
LEFT JOIN structure pc ON pc.structure_code = s.father_code
LEFT JOIN structure jv ON pc.father_code = jv.structure_code
    AND jv.father_code <> 'Wbs138Root'
WHERE fa.account_type_code IN (@FundingType)
    AND p.period_id BETWEEN @StartDate
        AND @FinishDate
    AND pe.code346 IN (@ActivePortfolio)
GROUP BY CASE 
        WHEN SUBSTRING(s.description, 13, 1) = '.'
            THEN LEFT(s.description, 17)
        ELSE LEFT(s.description, 12)
        END
    ,pn.description
    ,pe.code346
    ,CASE 
        WHEN ast.Description = 'WAIO'
            THEN anc.Description
        ELSE site.Description
        END
    ,CASE 
        WHEN SUBSTRING(jv.description, 5, 1) = ' '
            THEN LEFT(jv.description, 4)
        WHEN SUBSTRING(jv.description, 7, 1) = ' '
            THEN LEFT(jv.description, 6)
        ELSE jv.Description
        END
    ,pe.code79
    ,pe.code390
    ,CAST(p.period_start AS DATE)
ORDER BY 1
    ,CAST(p.period_start AS DATE)

【问题讨论】:

  • 好吧!这是一个可怕的查询!
  • 在 group by 子句后面添加 pe.short_name 字段。
  • 这个查询太可怕了。我讨厌成为必须主宰它的灵魂。对于您的问题,(a)通过将 pe.short_name 包装在 SUM() 或其他一些聚合函数中来聚合它,(b)将它添加到 group-by (您说您不想要)在这种情况下您有每个不同值的单独行,或 (c) 一起省略该字段。没有看到您的数据,很难告诉您什么是最好的。
  • 我很高兴不只是我这么想......我对 SQL 非常陌生,并且负责维护一堆报告......我没有写过这些报告。 ..我必须承认我正在为这些查询而苦苦挣扎..>我认为这只是我刚接触 SQL...非常感谢您的建议!
  • 我敢打赌,您在该错误消息中遗漏了一个 not。

标签: sql tsql


【解决方案1】:

试试这个:

SELECT CASE 
        WHEN SUBSTRING(s.description, 13, 1) = '.'
            THEN LEFT(s.description, 17)
        ELSE LEFT(s.description, 12)
        END WBSCode
    ,pe.short_name
    ,pn.description ProjectName
    ,(
        SELECT description
        FROM structure
        WHERE structure_code = pe.code346
        ) ActivePortfolio
    ,CASE 
        WHEN ast.Description = 'WAIO'
            THEN anc.Description
        ELSE site.Description
        END SiteFunction
    ,CASE 
        WHEN SUBSTRING(jv.description, 5, 1) = ' '
            THEN LEFT(jv.description, 4)
        WHEN SUBSTRING(jv.description, 7, 1) = ' '
            THEN LEFT(jv.description, 6)
        ELSE jv.Description
        END AS JointVenture
    ,(
        SELECT description
        FROM structure
        WHERE structure_code = pe.code79
        ) AssetClassification
    ,(
        SELECT description
        FROM structure
        WHERE structure_code = pe.code390
        ) InvestmentSize
    ,(
        SELECT RIGHT(import_code, 6)
        FROM structure
        WHERE structure_code = pe.code79
        ) AssetClassCode
    ,CAST(p.period_start AS DATE) Period_Start
    ,SUM(CASE 
            WHEN be.currency_code = 'AUD'
                AND @Currency = 'AUD'
                THEN be.amount
            WHEN be.currency_code = 'USD'
                AND @Currency = 'USD'
                THEN be.amount
            WHEN be.currency_code = 'AUD'
                AND @Currency = 'USD'
                THEN be.amount * ip.pfn_exch_rate_on_date(p.period_start, be.currency_code)
            WHEN be.currency_code = 'USD'
                AND @Currency = 'AUD'
                THEN be.amount * (ip.pfn_exch_rate_on_date(p.period_start, be.currency_code) / ip.pfn_exch_rate_on_date(p.period_start, 'AUD'))
            END) Amount
FROM fm_budget_entry be
JOIN fm_period p ON be.period_id = p.period_id
JOIN fm_budget_line bl ON be.line_id = bl.line_id
    AND bl.version_id = @FinancialVersion
JOIN fm_budget_line_attrib la ON la.line_id = bl.line_id
    AND la.line_attrib_code = 'Wbs138'
JOIN structure s ON la.line_attrib_value = s.structure_code
JOIN fm_account fa ON bl.account_code = fa.account_code
JOIN planning_entity pe ON pe.planning_code = bl.structure_code
JOIN structure ast ON ast.structure_code = pe.code713
    AND ast.Description IN (@Assets)
JOIN structure pn ON pn.structure_code = pe.planning_code
LEFT JOIN structure site ON site.structure_code = pe.code37
LEFT JOIN structure_tree tr ON tr.dsc_code = pe.planning_code
    AND tr.anc_Depth = 5
LEFT JOIN structure anc ON anc.structure_code = tr.anc_code
LEFT JOIN fm_version v ON v.version_id = bl.version_id
LEFT JOIN structure pc ON pc.structure_code = s.father_code
LEFT JOIN structure jv ON pc.father_code = jv.structure_code
    AND jv.father_code <> 'Wbs138Root'
WHERE fa.account_type_code IN (@FundingType)
    AND p.period_id BETWEEN @StartDate
        AND @FinishDate
    AND pe.code346 IN (@ActivePortfolio)
GROUP BY CASE 
        WHEN SUBSTRING(s.description, 13, 1) = '.'
            THEN LEFT(s.description, 17)
        ELSE LEFT(s.description, 12)
        END
    ,pe.short_name
    ,pn.description
    ,pe.code346
    ,CASE 
        WHEN ast.Description = 'WAIO'
            THEN anc.Description
        ELSE site.Description
        END
    ,CASE 
        WHEN SUBSTRING(jv.description, 5, 1) = ' '
            THEN LEFT(jv.description, 4)
        WHEN SUBSTRING(jv.description, 7, 1) = ' '
            THEN LEFT(jv.description, 6)
        ELSE jv.Description
        END
    ,pe.code79
    ,pe.code390
    ,CAST(p.period_start AS DATE)
ORDER BY 1
    ,CAST(p.period_start AS DATE)

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 2011-07-04
    • 2013-08-17
    • 2022-12-06
    相关资源
    最近更新 更多