列在选择列表中无效,因为它包含在聚合函数或GROUP BY子句中

时间:2018-04-20 02:53:49

标签: sql tsql

我知道这个主题已经涵盖了类似的问题,我已经查看了答案,但遗憾的是我无法确定如何应用于此查询。 我知道将'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)

1 个答案:

答案 0 :(得分:0)

试试这个:

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)