【问题标题】:Getting invalid identifier ORA-00904 in Pivot block在 Pivot 块中获取无效标识符 ORA-00904
【发布时间】: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');

【问题讨论】:

  • 请格式化代码。请阅读“如何提问部分”。非常感谢

标签: sql oracle pivot


【解决方案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) );

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2022-01-22
    • 2011-08-27
    • 2011-04-21
    • 2019-06-29
    相关资源
    最近更新 更多