15

欠落している値に参加したくないように見えるOracleでのクエリの作成に問題があります

私が持っているテーブルはこれです:

table myTable(refnum, contid, type)

values are:
1, 10, 90000
2, 20, 90000
3, 30, 90000
4, 20, 10000
5, 30, 10000
6, 10, 20000
7, 20, 20000
8, 30, 20000

私が求めているフィールドの内訳は次のとおりです。

select a.refnum from myTable a where type = 90000
select b.refnum from myTable b where type = 10000 and contid in (select contid from myTable where type = 90000)
select c.refnum from myTable c where type = 20000 and contid in (select contid from myTable where type = 90000)

私が求めているクエリの結果は次のとおりです。

a.refnum, b.refnum, c.refnum

私はこれがうまくいくと思いました:

select a.refnum, b.refnum, c.refnum
from myTable a 
left outer join myTable b on (a.contid = b.contid) 
left outer join myTable c on (a.contid = c.contid) 
where a.id_tp_cd = 90000
and b.id_tp_cd = 10000
and c.id_tp_cd = 20000

したがって、値は次のようになります。

1, null, 6
2, 4, 7
3, 5, 8

しかし、その唯一の戻り:

2, 4, 7
3, 5, 8

左結合は左側のすべての値を表示し、右側にnullを作成すると思いました。

ヘルプ :(

4

2 に答える 2

27

左結合は一致するものがない右に対してnullを返すと言っているのは正しいですが、where句にこの制限を追加すると、これらのnullが返されることを許可しません。

and b.id_tp_cd = 10000
and c.id_tp_cd = 20000

代わりに、これらを結合の「on」句に入れることができるはずです。そうすれば、右側の関連する行だけが返されます。

select a.refnum, b.refnum, c.refnum
from myTable a 
left outer join myTable b on (a.contid = b.contid and b.id_tp_cd = 10000) 
left outer join myTable c on (a.contid = c.contid and c.id_tp_cd = 20000) 
where a.id_tp_cd = 90000
于 2008-12-12T12:06:50.130 に答える
2

または ansi の代わりに Oracle 構文を使用する

select a.refnum, b.refnum, c.refnum
from myTable a, mytable b, mytable c
where a.contid=b.contid(+)
and a.contid=c.contid(+)
and a.type = 90000
and b.type(+) = 10000
and c.type(+) = 20000;


REFNUM     REFNUM     REFNUM
---------- ---------- ----------
     1                     6
     2          4          7
     3          5          8
于 2011-10-15T02:32:29.120 に答える