0

最初の免責事項:

  1. 私はプログラマーではありません。
  2. 「より高度な」数学を教えられたことがない
  3. 上のステートメントにもかかわらず、SQLを使用する必要がある場合があります。

今、私は同僚のaviewから aを作成する必要があります (4 つの s を使用していた人は、where 部分での使用方法がわからないように見えました...)、そして今ここにいます。selectunionor

UNION同じ結果セットを取得しながら最後を取り除く簡単な読みやすい方法はありますか?

前もって感謝します!

select a.prodt_cde,
      b.ccy,
      a.int_tabno,
      b.start_dn,
      b.end_dn,
      b.frte_term,
      b.base_id,
      b.ptvar,
      c.base_rate,
      c.desc_shnm,
      c.rel_day
 from linc.systwodb_ptico a, linc.systwodb_ptlfo b, linc.systwodb_baso c
 where a.prodt_cde in
      (select prodt_cde
         from linc.systwodb_ptmao
        where prodt_clas in (select prod_clas
                               from linc.systwodb_ramto
                              where main_type in (71, 72))
          and allow_dif in ('Y', 'M'))
  and a.int_type = 'LS'
  and a.int_tabno = b.int_tabno
  and b.ccy in
      (select ccy from linc.systwodb_ptmao where prodt_cde = a.prodt_cde)
  and b.base_id <> 0
  and b.base_id = c.base_id
  and b.ccy = c.ccy
  and ((b.end_dn = 0 and b.start_dn <= c.rel_day) or
      (b.end_dn <> 0 and b.start_dn <= c.rel_day and
      b.end_dn >= c.rel_day) or
      (b.start_dn > c.rel_day and not exists
       (select *
           from linc.systwodb_baso
          where base_id = b.base_id
            and ccy = b.ccy
            and rel_day = b.start_dn) and
       c.rel_day = (select NVL(max(rel_day), 0)
                       from linc.systwodb_baso
                      where base_id = b.base_id
                        and ccy = b.ccy
                        and rel_day < b.start_dn)))
-- 4. PTLFO.BASE_ID = 0, or cannot find BASO before PTLFO.START_DN 
union
select a.prodt_cde,
      b.ccy,
      a.int_tabno,
      b.start_dn,
      b.end_dn,
      b.frte_term,
      b.base_id,
      b.ptvar,
      0 as base_rate,
      ' ' as desc_shnm,
      0 as rel_day
 from linc.systwodb_ptico a, linc.systwodb_ptlfo b --, linc.systwodb_baso c
 where a.prodt_cde in
      (select prodt_cde
         from linc.systwodb_ptmao
        where prodt_clas in (select prod_clas
                               from linc.systwodb_ramto
                              where main_type in (71, 72))
          and allow_dif in ('Y', 'M'))
  and a.int_type = 'LS'
  and a.int_tabno = b.int_tabno
  and b.ccy in
      (select ccy from linc.systwodb_ptmao where prodt_cde = a.prodt_cde)
  and (b.base_id = 0 or not exists
       (select *
          from linc.systwodb_baso
         where base_id = b.base_id
           and ccy = b.ccy
           and rel_day <= b.start_dn))
;
4

1 に答える 1

2

これが何をすることになっているのか、大まかな説明を投稿していただけますか?ただし、このクエリは、何をしているのかを知らずに操作するのは非常に困難です。これらを組み合わせるための基本的なアプローチは、次のようにfrom句で明示的な結合を使用することです。

 from
    linc.systwodb_ptico a
    INNER JOIN linc.systwodb_ptlfo b ON a.int_tabno = b.int_tabno
    LEFT OUTER JOIN linc.systwodb_baso c ON -- some kind of horrible mess here

の左外側結合に注意してくださいsystwodb_baso。これが、他のクエリを排除するための重要なポイントです。これにより、から一致するレコードがない場合でも、結果セットに行が確実に含まれるようになりsystwodb_basoます。

アップデート:

外部結合からnull値を削除するには、次のCOALESCE関数を使用します。

select a.prodt_cde,
      b.ccy,
      a.int_tabno,
      b.start_dn,
      b.end_dn,
      b.frte_term,
      b.base_id,
      b.ptvar,
      COALESCE(c.base_rate, 0) AS base_rate,
      COALESCE(c.desc_shnm, ' ') AS desc_shnm,
      COALESCE(c.rel_day, 0) AS rel_day
于 2009-11-09T13:34:44.703 に答える