ORA-00904: неверный идентификатор в предложении WITH - факторинг подзапроса

Я получаю следующую ошибку для нижеуказанного запроса.

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 ответ

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; 
Другие вопросы по тегам