现有3个表, 以PJT_NO,VRB_NO 两个字段连接表。
1) A_TB : PJT_NO,VRB_NO,A_NO
2) B_TB : PJT_NO,VRB_NO,B_NO
3) C_TB : PJT_NO,VRB_NO,C_NO现在想得到这样的结果
1)如果 A_TB有 2个数据(就是 A_NO = 1,2), B_TB有 3个数据,C_TB有 4个数据。 得到下面数据
PJT_NO, VRB_NO, A_NO, B_NO, C_NO
---------------------------------------------------------------
P0001 V0 1 1 1
P0001 V0 2 2 2
P0001 V0 NULL 3 3
P0001 V0 NULL NULL 4
---------------------------------------------------------------- 2)如果 A_TB有 4个数据(就是 A_NO = 1,2), B_TB有 3个数据,C_TB有 2个数据。 得到下面数据
PJT_NO, VRB_NO, A_NO, B_NO, C_NO
---------------------------------------------------------------
P0001 V0 1 1 1
P0001 V0 2 2 2
P0001 V0 3 3 NULL
P0001 V0 4 NULL NULL
----------------------------------------------------------------
这个要求能实现吗?
谢谢大家了
1) A_TB : PJT_NO,VRB_NO,A_NO
2) B_TB : PJT_NO,VRB_NO,B_NO
3) C_TB : PJT_NO,VRB_NO,C_NO现在想得到这样的结果
1)如果 A_TB有 2个数据(就是 A_NO = 1,2), B_TB有 3个数据,C_TB有 4个数据。 得到下面数据
PJT_NO, VRB_NO, A_NO, B_NO, C_NO
---------------------------------------------------------------
P0001 V0 1 1 1
P0001 V0 2 2 2
P0001 V0 NULL 3 3
P0001 V0 NULL NULL 4
---------------------------------------------------------------- 2)如果 A_TB有 4个数据(就是 A_NO = 1,2), B_TB有 3个数据,C_TB有 2个数据。 得到下面数据
PJT_NO, VRB_NO, A_NO, B_NO, C_NO
---------------------------------------------------------------
P0001 V0 1 1 1
P0001 V0 2 2 2
P0001 V0 3 3 NULL
P0001 V0 4 NULL NULL
----------------------------------------------------------------
这个要求能实现吗?
谢谢大家了
1)
select a.pjt_no,a.vrb_no,a.a_no,b.b_no,c.c_no
from a_tb a,b_tb b,c_tb c
where a.pjt_no(+) = c.pjt_no
and b.pjt_no(+) = c.pjt_no
2)
select a.pjt_no,a.vrb_no,a.a_no,b.b_no,c.c_no
from a_tb a,b_tb b,c_tb c
where b.pjt_no(+) = a.pjt_no
and c.pjt_no(+) = a.pjt_no
select a.pjt_no,a.vrb_no,a.a_no,b.b_no,c.c_no
from a_tb a,b_tb b,c_tb c
where a.pjt_no(+) = c.pjt_no
and b.pjt_no(+) = c.pjt_no
2)
select a.pjt_no,a.vrb_no,a.a_no,b.b_no,c.c_no
from a_tb a,b_tb b,c_tb c
where b.pjt_no(+) = a.pjt_no
and c.pjt_no(+) = a.pjt_no
from a_tb a, b_tb b, c_tb c
where (a.no = b.no and a.no = c.no)
or (a.no = b.no and c.no is null)
or (a.no = c.no and b.no is null)
or (b.no = c.no and a.no is null)
or (a.no is null and b.no is null)
or (a.no is null and c.no is null)
or (b.no is null and c.no is null)
(select PJT_NO,VRB_NO,A_NO,B_NO from full A_TB join B_TB on A_TB.A_NO=B_TB.B_NO
)a join C_TB on a. A_NO=C_TB.C_NO
这个应该可以的,不过效率可能慢一些
1)
SELECT CB.PJT_NO, CB.VRB_NO, A_TB.A_NO, CB.B_NO, CB.C_NO
FROM (
SELECT C_TB.PJT_NO, C_TB.VRB_NO, B_TB.B_NO, C_TB.C_NO
FROM C_TB
LEFT JOIN B_TB
ON C_TB.C_NO = B_TB.B_NO
) CB
LEFT JOIN A_TB
ON CB.C_NO = A_TB.A_NO;2)同理
select PJT_NO,VRB_NO,A_NO,B_NO,C_NO from
(select PJT_NO,VRB_NO,A_NO,B_NO from A_TB join B_TB full on A_TB.A_NO=B_TB.B_NO
)a full join C_TB on a. A_NO=C_TB.C_NO
,NVL(NVL(A.VRB_NO,B.VRB_NO),C.VRB_NO) VRB_NO
,A.A_NO
,B.B_NO
,C.C_NO
FROM A_TB A
FULL OUTER JOIN B_TB B ON A.PJT_NO = B.PJT_NO AND A.VRB_NO = B.VRB_NO AND A.A_NO = B.B_NO
FULL OUTER JOIN C_TB C ON NVL(A.PJT_NO,B.PJT_NO) = C.PJT_NO AND NVL(A.VRB_NO,B.VRB_NO) = C.VRB_NO AND NVL(A.A_NO,B.B_NO) = C.C_NOPS:这个ORACLE8i(含)之前的版本好象不支持
(
select a.PJT_NO as PJT_NO,a.A_NO as A_NO,b.B_NO as B_NO from B b full join A a on a.PJT_NO=b.PJT_NO
)d
full join C c on d.PJT_NO=c.PJT_NO
select case when count(t2.t_PJT_NO)>count(c.PJT_NO) then t2.t_PJT_NO else c.PJT_NO end max_PJT_NO,t2.A_NO,t2.B_NO,c.C_NO
from
(
select
(case when count(A_PJT_NO)>count(B_PJT_NO) then A_PJT_NO else B_PJT_NO end) as t_PJT_NO,
t1.A_NO,
t1.B_NO
from
(select a.PJT_NO as A_PJT_NO,b.PJT_NO as B_PJT_NO,a.A_NO as A_NO,b.B_NO as B_NO from B b full join A a on a.PJT_NO=b.PJT_NO)t1
group by A_PJT_NO,B_PJT_NO,A_NO,B_NO
) t2
full join C c on t2.t_PJT_NO=c.PJT_NO
group by t2.t_PJT_NO,c.PJT_NO,t2.A_NO,t2.B_NO,c.C_NO
SELECT DECODE(A_TB.PJT_NO,'P0001','P0001','P0001'),A_NO,B_NO,C_NO
FROM (A_TB FULL OUTER JOIN B_TB ON A_NO=B_NO) FULL OUTER JOIN C_TB ON A_NO=C_NO OR B_NO=C_NO
;
DECOD A_NO B_NO C_NO
----- ---------- ---------- ----------
P0001 1 1 1
P0001 2 2 2
P0001 3 3
P0001 4第2种情况:A_TB 有4个数,B_TB有3个数,C_TB有2个数。现在仍用上面建立的三个表,即可以把C_TB作为第1个表,B_TB作为第2个表,而A_TB作为第3表来看
SELECT DECODE(A_TB.PJT_NO,'P0001','P0001','P0001'),C_NO,B_NO,A_NO
FROM (C_TB FULL OUTER JOIN B_TB ON C_NO=B_NO) FULL OUTER JOIN A_TB ON A_NO=C_NO OR A_NO=B_NO
;
DECOD C_NO B_NO A_NO
----- ---------- ---------- ----------
P0001 1 1 1
P0001 2 2 2
P0001 3 3
P0001 4
我是使用全外连接...FULL OUTER JOIN...ON... 。另外,不管那三个表的记录量是哪一种情况,都是用同一句SQL的,我上面的是愉懒了的!
呵呵
select decode(A_TB.PJT_NO,'P0001','P0001','P0001') PJT_NO ,decode(A_TB.VRB_NO,'VO','VO','VO') VRB_NO,A_NO,B_NO,C_NO
from
A_TB full join B_TB on A_NO = B_NO
full join C_TB on B_NO = C_NO;
select decode(C_TB.PJT_NO,'P0001','P0001','P0001') PJT_NO ,decode(C_TB.VRB_NO,'VO','VO','VO') VRB_NO,A_NO,B_NO,C_NO
from
C_TB full join B_TB on C_NO = B_NO
full join A_TB on B_NO = A_NO;
比如A表只有2个数据,那么它的pjt_no字段的值也只有2个
那么查询输出的应该是哪个表的pjt_no和vrb_no,才不会使这两个字段出现null
如果像题目中,Pjt_no和vrb_no的值都一样的话,我的解答如下
create table test_a(pjt_no varchar2(10) not null,vrb_no varchar2(10) not null,a_no number);
create table test_b(pjt_no varchar2(10) not null,vrb_no varchar2(10) not null,b_no number);
create table test_c(pjt_no varchar2(10) not null,vrb_no varchar2(10) not null,c_no number);insert into test_a values('p0001','v0',1);
insert into test_a values('p0001','v0',2);
insert into test_b values('p0001','v0',1);
insert into test_b values('p0001','v0',2);
insert into test_b values('p0001','v0',3);
insert into test_b values('p0001','v0',4);
insert into test_c values('p0001','v0',1);
insert into test_c values('p0001','v0',2);
insert into test_c values('p0001','v0',3);select case when a.pjt_no is not null then a.pjt_no
when b.pjt_no is not null then b.pjt_no
else c.pjt_no end pjt_no,
case when a.vrb_no is not null then a.vrb_no
when b.vrb_no is not null then b.vrb_no
else c.vrb_no end vrb_no,
a.a_no,b.b_no,c.c_no from test_a a full join test_b b on a.a_no=b.b_no full join test_c c on b.b_no=c.c_no;PJT_NO VRB_NO A_NO B_NO C_NO
p0001 v0 1 1 1
p0001 v0 2 2 2
p0001 v0 3 3
p0001 v0 4
select case when (select count(pjt_no) from test_a)>=(select count(pjt_no) from test_b) and (select count(pjt_no) from test_a)>=(select count(pjt_no) from test_c) then a.pjt_no
when (select count(pjt_no) from test_b)>=(select count(pjt_no) from test_a) and (select count(pjt_no) from test_b)>=(select count(pjt_no) from test_c) then b.pjt_no
else c.pjt_no end pjt_no,
case when (select count(vrb_no) from test_a)>=(select count(vrb_no) from test_b) and (select count(vrb_no) from test_a)>=(select count(vrb_no) from test_c) then a.vrb_no
when (select count(vrb_no) from test_b)>=(select count(vrb_no) from test_a) and (select count(vrb_no) from test_b)>=(select count(vrb_no) from test_c) then b.vrb_no
else c.vrb_no end vrb_no,
a.a_no,b.b_no,c.c_no from test_a a full join test_b b on a.a_no=b.b_no full join test_c c on b.b_no=c.c_noPJT_NO VRB_NO A_NO B_NO C_NO
p0001 v0 1 1 1
p0001 v0 2 2 2
p0001 v0 3 3
p0001 v0 4