简体   繁体   中英

ORA-00904: invalid identifier in WITH clause - subquery factoring

I am getting following error, for the bellow query.

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

It seems BKG_ITEM and BKG are not identified by the inner clauses. What am I missing here and Is there a way to do what I am trying to do? ( I am trying to optimise the query using WITH clause)

    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;

Thank you in advance!

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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM