PL-SQL:ORA-00904 - 标识符无效 - Select and Pivot 子句中的列

Posted

技术标签:

【中文标题】PL-SQL:ORA-00904 - 标识符无效 - Select and Pivot 子句中的列【英文标题】:PL-SQL: ORA-00904 - Invalid Identifier - Column in Select and Pivot Clause 【发布时间】:2020-03-11 17:21:12 【问题描述】:

具有可用于在下面创建输出的代码,并且无法在我的一生中调试枢轴以将“税务状态”带到每个都有自己的列。我删除了“ORDER”和“GROUP”子句并尝试将其作为基准,但它仍然产生“ORA-00904:“Tax Status”:无效标识符”。根据我所做的,它有时会在初始选择时进行,其他人会对枢轴以及我是否使用单引号或双引号感到愤怒。

枢轴挣扎:

pivot
(
    min('Total Value') 
    for ("Tax Status")
    in ('Exempt from Taxation' as "Exempt", 'Taxable' as "Taxable", 'Tax Agreement - Operator of a Public Utility' as "Tax Agreement",
    '"Assessed Person" Tax Agreement' as "Tax Agreement", 'Grant in Place of Tax' as "Grant", 'Council Tax Cancellation or Refund' as "Council Cancel/Refund")
)

产生以下输出的工作查询:

SELECT "Team", "Tax Status", sum("Total Value") "Total Value"
FROM 
    (
    select (A.account_roll_number) "Roll Number", ALU.DESCRIPTION "Assessor", A.account_total_property_value as "Total Value",
    TaxLU.DESCRIPTION "Tax Status",
    
    CASE
        when A.assessor_id in ('ATHENDRATA', 'BRTHOMPSON', 'FTACIUNE', 'HPHILLIPS', 'JDCHU', 'JRYOUNG1', 'MHARTMANN', 'NCCHAN', 'RLEE5', 'SBERZINS') then 'Industrial'
        when A.assessor_id in ('ASMTDWA','ASMTEB', 'ASMTWS', 'BBROCKLEBANK', 'CCHICHAK', 'CYMAU', 'GJONES4', 'IYPAU', 'JTGREER', 'KHOUSTON', 'LGMORRISON', 'MMCKENZIE1', 'MWALZ', 'SKUANG', 'STBAZIN', 'TKNGUYEN1', 'ASHIELDS') then 'Single Residential'
        when A.assessor_id in ('BTANG', 'CMACMILLAN1', 'DGENCARELLI', 'EWU1', 'JWEHLER', 'LMDUNBAR', 'LWONG4', 'MGULOWSKA', 'RLEE1', 'SHAMMOUD', 'SLTURNER', 'YWANG') then 'Multi Residential'
        when A.assessor_id in ('CMULENGA', 'EPOPOVICS', 'GFULLER', 'IMCDERMOTT', 'JERMUBE', 'JNSTEVENSON', 'JPLEPINE', 'KBUCKRY', 'KDALMHJELL', 'NPHAM1', 'PGKERSEY', 'SMSAMPLE') then 'Special Purpose and Land'
        when A.assessor_id in ('ASMTHN', 'DCARSON', 'DLIDGREN', 'DMCCORD', 'EBORISENKO', 'HYAU1', 'MCTRIMBLE', 'RJTHARAKAN', 'TBJOHNSON1', 'VWONG1', 'WGIBBS', 'YYE', 'AVPETERS') then 'Office'
        when A.assessor_id in ('AKEAST', 'BLTHOMPSON', 'BNELSON1', 'JCRUJI', 'JWONG1', 'KGARDINER', 'KMHAUT', 'NTNGUYEN', 'RTLUCHAK', 'SGILL3', 'THEGER1', 'TJLUDLOW', 'ZRGEIB') then 'Retail'
        else 'Other'
    END as "Team"
    
    from REP_DBA.AB000_ACCOUNT A
    
    join REP_DBA.LU_ASMT_ASSIGN_ASSESSOR ALU
    on A.assessor_id = ALU.code
    
    -- Decode the Tax Status from the LU Table
    join rep_dba.LU_ASMT_ACCT_TAX_STATUS TaxLU
    on TaxLU.CODE = A.account_tax_status
    
    where A.rollyear = :rollyear
    and A.account_type = 'P'
    and A.account_status = 'AP'
    and A.account_total_property_value is not null
    ORDER BY account_total_property_value DESC
)
GROUP BY "Team", "Tax Status"
ORDER BY "Team"

透视前的输出

【问题讨论】:

【参考方案1】:

PIVOT 中的列必须使用双引号,如下所示:

Select * from
(
SELECT "Team", "Tax Status", sum("Total Value") "Total Value"
FROM 
    (
    select (A.account_roll_number) "Roll Number", ALU.DESCRIPTION "Assessor", A.account_total_property_value as "Total Value",
    TaxLU.DESCRIPTION "Tax Status",

    CASE
        when A.assessor_id in ('ATHENDRATA', 'BRTHOMPSON', 'FTACIUNE', 'HPHILLIPS', 'JDCHU', 'JRYOUNG1', 'MHARTMANN', 'NCCHAN', 'RLEE5', 'SBERZINS') then 'Industrial'
        when A.assessor_id in ('ASMTDWA','ASMTEB', 'ASMTWS', 'BBROCKLEBANK', 'CCHICHAK', 'CYMAU', 'GJONES4', 'IYPAU', 'JTGREER', 'KHOUSTON', 'LGMORRISON', 'MMCKENZIE1', 'MWALZ', 'SKUANG', 'STBAZIN', 'TKNGUYEN1', 'ASHIELDS') then 'Single Residential'
        when A.assessor_id in ('BTANG', 'CMACMILLAN1', 'DGENCARELLI', 'EWU1', 'JWEHLER', 'LMDUNBAR', 'LWONG4', 'MGULOWSKA', 'RLEE1', 'SHAMMOUD', 'SLTURNER', 'YWANG') then 'Multi Residential'
        when A.assessor_id in ('CMULENGA', 'EPOPOVICS', 'GFULLER', 'IMCDERMOTT', 'JERMUBE', 'JNSTEVENSON', 'JPLEPINE', 'KBUCKRY', 'KDALMHJELL', 'NPHAM1', 'PGKERSEY', 'SMSAMPLE') then 'Special Purpose and Land'
        when A.assessor_id in ('ASMTHN', 'DCARSON', 'DLIDGREN', 'DMCCORD', 'EBORISENKO', 'HYAU1', 'MCTRIMBLE', 'RJTHARAKAN', 'TBJOHNSON1', 'VWONG1', 'WGIBBS', 'YYE', 'AVPETERS') then 'Office'
        when A.assessor_id in ('AKEAST', 'BLTHOMPSON', 'BNELSON1', 'JCRUJI', 'JWONG1', 'KGARDINER', 'KMHAUT', 'NTNGUYEN', 'RTLUCHAK', 'SGILL3', 'THEGER1', 'TJLUDLOW', 'ZRGEIB') then 'Retail'
        else 'Other'
    END as "Team"

    from REP_DBA.AB000_ACCOUNT A

    join REP_DBA.LU_ASMT_ASSIGN_ASSESSOR ALU
    on A.assessor_id = ALU.code

    -- Decode the Tax Status from the LU Table
    join rep_dba.LU_ASMT_ACCT_TAX_STATUS TaxLU
    on TaxLU.CODE = A.account_tax_status

    where A.rollyear = :rollyear
    and A.account_type = 'P'
    and A.account_status = 'AP'
    and A.account_total_property_value is not null
    -- ORDER BY account_total_property_value DESC -- not needed
  )
GROUP BY "Team", "Tax Status"
)
pivot
(
    min("Total Value")  -- here
    for ("Tax Status")
    in ('Exempt from Taxation' as "Exempt", 'Taxable' as "Taxable", 
        'Tax Agreement - Operator of a Public Utility' as "Tax Agreement", -- duplicate
        '"Assessed Person" Tax Agreement' as "Tax Agreement 1", -- changed alias, added 1
        'Grant in Place of Tax' as "Grant", 'Council Tax Cancellation or Refund' as "Council Cancel/Refund")
)

干杯!!

【讨论】:

尝试过,但还没有运气。我是否将sum("Total Value") "TotalValue" 留在选择中?我也放了 min 但现在认为这可能是不正确的。还将将任何“TotalValue”重命名为“Total Value”以保持一致。 感谢您的帮助。我很确定它几乎在那里,从你那里复制了信息。我看到了 select * from () 和 group by 的想法。现在说“00918. 00000 - “列定义不明确” ...“行错误:1 列:8”所以它不喜欢 select * from。 知道了.. pivot 子句 --> 在子句as "Tax Agreement" 中使用了两次。更改其名称之一。查看更新的答案。 非常感谢!我看到相应的更改/重命名它们并且运行良好。非常感谢。

以上是关于PL-SQL:ORA-00904 - 标识符无效 - Select and Pivot 子句中的列的主要内容,如果未能解决你的问题,请参考以下文章

错误报告:SQL 错误:ORA-00904::无效标识符 00904。00000 - “%s:无效标识符”

ORA-00904: 无效标识符 00904. 00000 - "%s: 无效标识符"

为啥我会收到:[Oracle][ODBC][Ora]ORA-00904:标识符无效

ORA-00904 无效标识符错误

SQL 错误 ORA-00904:标识符无效

ORA-00904 标识符无效