A汽车 需要轮胎,方向盘 方向盘,轮胎 方向盘需要螺丝
B汽车 需要轮胎,方向盘 方向盘,轮胎 方向盘,轮胎都需要螺丝所以现在表的记录是:
A:
ID ParentID 物料编码 物料名称
1 0 qc 汽车
2 1 fxp 方向盘
3 1 lt 轮胎
4 2 ls 螺丝 B:
ID ParentID 物料编码 物料名称
1 0 qc 汽车
2 1 fxp 方向盘
3 1 lt 轮胎
4 2 ls 螺丝
5 3 ls 螺丝
我现在select * from A a full join B b on a.物料编码 = b.物料编码
会出现现在的情况
qc 汽车 qc 汽车
fxp 方向盘 fxp 方向盘
lt 轮胎 lt 轮胎
ls 螺丝 ls 螺丝
ls 螺丝 ls 螺丝
我先要的结果是
qc 汽车 qc 汽车
fxp 方向盘 fxp 方向盘
lt 轮胎 lt 轮胎
ls 螺丝 ls 螺丝
null null ls 螺丝
请问如何解决?
B汽车 需要轮胎,方向盘 方向盘,轮胎 方向盘,轮胎都需要螺丝所以现在表的记录是:
A:
ID ParentID 物料编码 物料名称
1 0 qc 汽车
2 1 fxp 方向盘
3 1 lt 轮胎
4 2 ls 螺丝 B:
ID ParentID 物料编码 物料名称
1 0 qc 汽车
2 1 fxp 方向盘
3 1 lt 轮胎
4 2 ls 螺丝
5 3 ls 螺丝
我现在select * from A a full join B b on a.物料编码 = b.物料编码
会出现现在的情况
qc 汽车 qc 汽车
fxp 方向盘 fxp 方向盘
lt 轮胎 lt 轮胎
ls 螺丝 ls 螺丝
ls 螺丝 ls 螺丝
我先要的结果是
qc 汽车 qc 汽车
fxp 方向盘 fxp 方向盘
lt 轮胎 lt 轮胎
ls 螺丝 ls 螺丝
null null ls 螺丝
请问如何解决?
from b left join a on b.ID = a.ID
insert into a values(1 ,0 ,'qc' , '汽车')
insert into a values(2 ,1 ,'fxp', '方向盘')
insert into a values(3 ,1 ,'lt' , '轮胎')
insert into a values(4 ,2 ,'ls' , '螺丝')
create table B(ID int,ParentID int,物料编码 varchar(10),物料名称 varchar(10))
insert into B values(1 ,0 ,'qc' , '汽车')
insert into B values(2 ,1 ,'fxp', '方向盘')
insert into B values(3 ,1 ,'lt' , '轮胎')
insert into B values(4 ,2 ,'ls' , '螺丝')
insert into B values(5 ,3 ,'ls' , '螺丝') goselect a.物料编码 ,a.物料名称 , b.物料编码 , b.物料名称
from b left join a on b.id = a.iddrop table a , b/*
物料编码 物料名称 物料编码 物料名称
---------- ---------- ---------- ----------
qc 汽车 qc 汽车
fxp 方向盘 fxp 方向盘
lt 轮胎 lt 轮胎
ls 螺丝 ls 螺丝
NULL NULL ls 螺丝(所影响的行数为 5 行)
*/
insert into A select 1,0,'qc','汽车'
insert into A select 2,1,'fxp','方向盘'
insert into A select 3,1,'lt','轮胎'
insert into A select 4,2,'ls','螺丝' create table B(ID int,ParentID int,物料编码 varchar(10),物料名称 varchar(20))
insert into B select 1,0,'qc','汽车'
insert into B select 2,1,'fxp','方向盘'
insert into B select 3,1,'lt','轮胎'
insert into B select 4,2,'ls','螺丝'
insert into B select 5,3,'ls','螺丝'go
select * from A a full join B b on a.id = b.id
/*
ID ParentID 物料编码 物料名称 ID ParentID 物料编码 物料名称
----------- ----------- ---------- -------------------- ----------- ----------- ---------- --------------------
1 0 qc 汽车 1 0 qc 汽车
2 1 fxp 方向盘 2 1 fxp 方向盘
3 1 lt 轮胎 3 1 lt 轮胎
4 2 ls 螺丝 4 2 ls 螺丝
NULL NULL NULL NULL 5 3 ls 螺丝(5 行受影响)*/
go
drop table A,B
--2005create table A(ID int,ParentID int,物料编码 varchar(10),物料名称 varchar(20))
insert into A select 1,0,'qc','汽车'
insert into A select 2,1,'fxp','方向盘'
insert into A select 3,1,'lt','轮胎'
insert into A select 4,2,'ls','螺丝'
go
create table B(ID int,ParentID int,物料编码 varchar(10),物料名称 varchar(20))
insert into B select 1,0,'qc','汽车'
insert into B select 2,1,'fxp','方向盘'
insert into B select 3,1,'lt','轮胎'
insert into B select 4,2,'ls','螺丝'
insert into B select 5,3,'ls','螺丝'
go;with t1 as
(
select *,rid=row_number() over (partition by 物料编码 order by id)
from a
),t2 as
(
select *,rid=row_number() over (partition by 物料编码 order by id)
from b
)select a.物料编码 ,a.物料名称 , b.物料编码 , b.物料名称
from t1 a full join t2 b on a.物料编码 = b.物料编码 and a.rid = b.riddrop table A,B/*******************物料编码 物料名称 物料编码 物料名称
---------- -------------------- ---------- --------------------
fxp 方向盘 fxp 方向盘
ls 螺丝 ls 螺丝
NULL NULL ls 螺丝
lt 轮胎 lt 轮胎
qc 汽车 qc 汽车(5 行受影响)