【问题标题】:ORA-00904: invalid identifier in WITH clause - subquery factoringORA-00904: WITH 子句中的标识符无效 - 子查询分解
【发布时间】:2013-09-24 07:58:38
【问题描述】:

以下查询出现以下错误。

ORA-00904: "BKG_ITEM"."ITEM_NO": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action: Error at Line: 11 Column: 60

似乎 BKG_ITEM 和 BKG 没有被内部子句标识。我在这里错过了什么,有没有办法做我想做的事情? (我正在尝试使用 WITH 子句优化查询)

    select main_query.*
        , RANK() OVER ( ORDER BY booking_id, product_code, item_no ) AS RNK
        from
          (select bkg_item.*,
          (
          CASE (bkg_item.product_code)
            WHEN (  'TOU'  ) THEN
              ( 
              WITH rtb_dep_loc as ( select departure_location from res_tour_booking rtb 
                                                  where  rtb.booking_id = bkg_item.booking_id
                                                  and rtb.item_no = bkg_item.item_no
                                                  and rtb.product_code = 'TOU'
                                                  and rownum = 1
                                   )            
              select city.name name from city
                     inner join location lc on lc.city = city.code
                     inner join rtb_dep_loc on lc.name = rtb_dep_loc.departure_location
              union
              select city.name name from city
                     inner join airport lc on lc.city = city.code
                     inner join rtb_dep_loc on lc.name = rtb_dep_loc.departure_location
              union
              select city.name name from city
                     inner join supplier lc on lc.city = city.code
                     inner join rtb_dep_loc on lc.name = rtb_dep_loc.departure_location
              )
              else ''
              end
          ) as city

   from
      (select rb.* from res_booking rb where rb.booking_id  > 0 ) bkg 
     inner join
      (select rbi.* from res_booking_item rbi where rbi.booking_id > 0 and rbi.product_code not in ( 'OWA' ) ) bkg_item 
     on bkg_item.booking_id = bkg.booking_id
   )main_query;

提前谢谢你!

【问题讨论】:

    标签: sql oracle with-clause subquery-factoring


    【解决方案1】:
    WITH
    bkg as (select rb.* from res_booking rb where rb.booking_id  > 0 ),
    bkg_item as (select rbi.* from res_booking_item rbi where rbi.booking_id > 0 and rbi.product_code not in ( 'OWA' ) ),
    select *
            , RANK() OVER ( ORDER BY booking_id, product_code, item_no ) AS RNK
            from
              (select bkg_item.*,
              (
              CASE (bkg_item.product_code)
                WHEN (  'TOU'  ) THEN
                  ( 
                  WITH rtb_dep_loc as ( select departure_location from res_tour_booking rtb 
                                                      where  rtb.booking_id = bkg_item.booking_id
                                                      and rtb.item_no = bkg_item.item_no
                                                      and rtb.product_code = 'TOU'
                                                      and rownum = 1
                                       )            
                  select city.name name from city
                         inner join location lc on lc.city = city.code
                         inner join rtb_dep_loc on lc.name = rtb_dep_loc.departure_location
                  union
                  select city.name name from city
                         inner join airport lc on lc.city = city.code
                         inner join rtb_dep_loc on lc.name = rtb_dep_loc.departure_location
                  union
                  select city.name name from city
                         inner join supplier lc on lc.city = city.code
                         inner join rtb_dep_loc on lc.name = rtb_dep_loc.departure_location
                  )
                  else ''
                  end
              ) as city
     from bkg inner join bkg_item 
         on bkg_item.booking_id = bkg.booking_id; 
    

    【讨论】:

    • 我更正了您答案中的几个语法错误并运行了它,但我仍然收到相同的消息。如果你能解释它背后的原因,我也许可以找到答案。 (由于 6 个字符的编辑限制,我还添加了 main_query 别名)
    • 我的意思是你回答背后的原因
    猜你喜欢
    • 2014-03-02
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2023-01-12
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多