当前位置: 代码迷 >> Sql Server >> 急用!请教怎么修改代码
  详细解决方案

急用!请教怎么修改代码

热度:63   发布时间:2016-04-24 10:39:42.0
急用!请问如何修改代码?
--> 测试数据[TB1]
if object_id('[TB1]') is not null drop table [TB1]
go 
create table [TB1]([BLU] nvarchar(6),[RepN] int)
insert [TB1]
select '01 08', 434 union all
select '01 04', 477 union all
select '07 12', 519 union all
select '07 11', 549 union all
select '08 11', 560 union all
select '04 11', 568 union all
select '01 07', 581 union all
select '04 12', 598 union all
select '01 06', 601 union all
select '10 11', 609 union all
select '06 10', 625 union all
select '02 03', 634 union all
select '06 08', 646 union all
select '03 08', 652 
--> 测试数据[TB2]
if object_id('[TB2]') is not null drop table [TB2]
go 
create table [TB2]([BLU1] nvarchar(4),[RepN1] int)
insert [TB2]
select '02',5655 union all
select '03',25026 union all
select '04',29502 union all
select '05',89886 union all
select '06',71380 union all
select '07',109676 union all
select '08',132257 union all
select '09',201662 union all
select '10',245659 union all
select '11',140627 union all
select '12',155814

--> 测试数据[TB3]
if object_id('[TB3]') is not null drop table [TB3]
go 
create table [TB3]([BLU2] nvarchar(4),[RepN2] int)
insert [TB3]
select '01',133955 union all
select '02',147485 union all
select '03',153295 union all
select '04',166689 union all
select '05',188967 union all
select '06',138090 union all
select '07',107990 union all
select '08',60633 union all
select '09',68339 union all
select '10',26799 union all
select '11',14902
--> 测试数据[TB4]
if object_id('[TB4]') is not null drop table [TB4]
go 
create table [TB4]([BLUT] nvarchar(4),[RepNT] int)
insert [TB4]
select '1 ', 133955 union all
select '2 ', 153140 union all
select '11 ', 155529 union all
select '12 ', 155814 union all
select '3 ', 178321 union all
select '8 ', 192890 union all
select '4 ', 196191 union all
select '6 ', 209470 union all
select '7 ', 217666 union all
select '9 ', 270001 union all
select '10 ', 272458 union all
select '5 ', 278853 
--------------生成数据--------------------------
select
id=row_number()over(order by (select 1)),
a.*,b.BLU1,b.RepN1,c.BLU2,c.RepN2,d.BLUT,d.RepNT
from tb1 a 
left join tb2 b on CHARINDEX(a.BLU+' ',b.BLU1+' ')>0
left join tb3 c on CHARINDEX(a.BLU+' ',c.BLU2+' ')>0
left join tb4 d   
on d.BLUT=b.BLU1 or d.BLUT=c.BLU2


执行的结果是
id	BLU	RepN	BLU1	RepN1	BLU2	RepN2	BLUT	RepNT
1 01 08 434 NULL NULL NULL NULL NULL NULL
2 01 04 477 NULL NULL NULL NULL NULL NULL
3 07 12 519 NULL NULL NULL NULL NULL NULL
4 07 11 549 NULL NULL NULL NULL NULL NULL
5 08 11 560 NULL NULL NULL NULL NULL NULL
6 04 11 568 NULL NULL NULL NULL NULL NULL
7 01 07 581 NULL NULL NULL NULL NULL NULL
8 04 12 598 NULL NULL NULL NULL NULL NULL
9 01 06 601 NULL NULL NULL NULL NULL NULL
10 10 11 609 NULL NULL NULL NULL NULL NULL
11 06 10 625 NULL NULL NULL NULL NULL NULL
12 02 03 634 NULL NULL NULL NULL NULL NULL
13 06 08 646 NULL NULL NULL NULL NULL NULL
14 03 08 652 NULL NULL NULL NULL NULL NULL


实际上我需要的是

------解决方案--------------------
;
with
wang1 as (select rowid=ROW_NUMBER() over(order by getdate()),* from tb1),
wang2 as (select rowid=ROW_NUMBER() over(order by getdate()),* from tb2),
wang3 as (select rowid=ROW_NUMBER() over(order by getdate()),* from tb3),
wang4 as (select rowid=ROW_NUMBER() over(order by getdate()),* from tb4)

select * 
from wang1 left join wang2 on wang1.rowid=wang2.rowid
left join wang3 on wang1.rowid=wang3.rowid
left join wang4 on wang1.rowid=wang4.rowid
  相关解决方案