ORA-00904:WITH子句中的无效标识符 - 子查询因子分解

时间:2013-09-24 07:58:38

标签: sql oracle with-clause subquery-factoring

对于下面的查询,我收到了以下错误。

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;

提前谢谢!

1 个答案:

答案 0 :(得分:0)

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;