在 Pivot 块中获取无效标识符 ORA-00904

Posted

技术标签:

【中文标题】在 Pivot 块中获取无效标识符 ORA-00904【英文标题】:Getting invalid identifier ORA-00904 in Pivot block 【发布时间】:2020-03-08 04:22:23 【问题描述】:

我在以下数据透视块的查询中得到无效标识符,请帮助解决这个问题

select cif_id,
       to_char(tran_date,'MON-YYYY'),
       part_tran_type ,
       tran_amt
from CUSTOM.HTD_OCT_DEC19 H,
            tbaadm.gam 
pivot
(count(1),
 sum(tran_amt)
for **part_tran_type** in ('D','C'))
where h.tran_date ='24-DEC-2019' 
  and h.PSTD_FLG='Y' 
  and h.DEL_FLG='N' 
  and gam.cust_id='D46478329' 
  and h.cust_id=gam.cust_id 
  and rpt_code in ('20211','20212','20270','20271','20410','20420','20440',
                   '20501','20502','20504','60202');

【问题讨论】:

请格式化代码。请阅读“如何提问部分”。非常感谢 【参考方案1】:

我认为您没有正确使用PIVOT。尝试使用以下代码:

SELECT * FROM
    (
        SELECT
            CIF_ID,
            TO_CHAR(TRAN_DATE, 'MON-YYYY') as TRAN_DATE,
            PART_TRAN_TYPE,
            TRAN_AMT
        FROM CUSTOM.HTD_OCT_DEC19 H, TBAADM.GAM
        WHERE
            H.TRAN_DATE = '24-DEC-2019'
            AND H.PSTD_FLG = 'Y'
            AND H.DEL_FLG = 'N'
            AND GAM.CUST_ID = 'D46478329'
            AND H.CUST_ID = GAM.CUST_ID
            AND RPT_CODE IN (
                '20211', '20212', '20270', '20271', '20410', '20420',
                '20440', '20501', '20502', '20504', '60202'
            )
    ) PIVOT (
        COUNT ( 1 ) AS CT, SUM ( TRAN_AMT ) AS SM
        FOR PART_TRAN_TYPE
        IN ( 'D', 'C' )
    )

干杯!!

【讨论】:

执行相同操作后,出现错误 ORA-00918 : column ambiguously defined【参考方案2】:

如下更改查询后现在工作:

select * from ( select gam.cif_id,to_char(tran_date,'MON-YYYY'),h.part_tran_type ,h.tran_amt from CUSTOM.HTD_OCT_DEC19 H,tbaadm.gam where h.tran_date ='24-DEC-2019' and h.PSTD_FLG='Y' and h.DEL_FLG='N' and gam.cif_id='D46478329' and h.cust_id=gam.cust_id and h.rpt_code in ('20211','20212','20270','20271','20410','20420','20440','20501','20502','20504','60202') ) pivot ( count(1) as TXNCOUNT, sum(tran_amt) as TXNSUM for part_tran_type in ('D' as DEBIT,'C' as CREDIT) );

【讨论】:

以上是关于在 Pivot 块中获取无效标识符 ORA-00904的主要内容,如果未能解决你的问题,请参考以下文章

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

如何在 Oracle 11 中取消透视单行?

在动态表上获取“ORA00904:无效标识符”

使用 db_link 插入获取无效标识符错误

在文本块中获取相同的正则表达式组

PostgreSQL:块中的无效页眉