0

次のように、ユニオンを使用した2つのクエリがあります。

     select   '00/00/0000' as payment_date , h1.customer_no
     from payments h1
     where not exists ( select 1 from payments h2 where h2.customer_no = h1.customer_no and h2.ctype = 'CASH' ) 
     and  h1.customer_no = 400
     group by h1.customer_no

     union

     select to_char(h1.payment_date, 'MM/DD/YYYY') , h1.customer_no
      from payments h1 inner   join ( select customer_no, max(payment_date ) as max_date from payments where ctype = 'CASH'  group by customer_no ) subQ
     on  ( h1.customer_no = subQ.customer_no
           and h1.payment_date = subQ.max_date   ) 
           and  h1.customer_no = 400
     group by h1.payment_date, h1.customer_no 

ここで、このユニオンを別のクエリで使用したいと考えています。

  select * from (

    select   '00/00/0000' as payment_date , h1.customer_no
    from payments h1
    where not exists ( select 1 from payments h2 where h2.customer_no = h1.customer_no and h2.ctype = 'CASH' ) 
    and  h1.customer_no = p.customer_no 
    group by h1.customer_no

    union

    select to_char(h1.payment_date, 'MM/DD/YYYY') , h1.customer_no
    from payments h1 inner   join ( select customer_no, max(payment_date ) as max_date from payments where ctype = 'CASH'  group by customer_no ) subQ
    on  ( h1.customer_no = subQ.customer_no
    and h1.payment_date = subQ.max_date   ) 
    and  h1.customer_no = p.customer_no 
    group by h1.payment_date, h1.customer_no ) sq,

  payments p
  where  p.customer_no = 400
  and sq.customer_no = p.customer_no 

これを実行すると、ORA-00904: "P"."CUSTOMER_NO": 無効な識別子が表示されます。h1.customer_no を外部クエリ customer_no に結合する必要があります。

ランク付きのクエリをいくつか見ましたが、よくわかりませんでした。内側のクエリを外側のクエリと結合するにはどうすればよいですか?

前もって感謝します。

4

1 に答える 1

0

支払いテーブルにcustomer_no列がありますか?それはあなたのエラーが示しているもののようです。

サブクエリに参加する方法については、因数分解されたサブクエリを調べることをお勧めします。できるよ:

WITH z AS (
  SELECT ...
  UNION
  SELECT ...
), y AS (
  SELECT ...
)
SELECT ...
FROM y
JOIN z ON y.x = z.x
JOIN some_other_table t ON z.a = t.a 
于 2013-02-26T22:45:38.447 に答える