27,579
社区成员
发帖
与我相关
我的任务
分享
表1
Name1 Amount1 Weight1
N1 10 0.5
N2 7 0.3
表2
Name2 Amount2 Weight2
N6 2 0.4
N4 3 0.58
C2 5 1.2
D9 8 2.1
连接后为
Name1 Amount1 Weight1 Name2 Amount2 Weight2
N1 10 0.5 N6 2 0.4
N2 7 0.3 N4 3 0.58
C2 5 1.2
D9 8 2.1
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([code] varchar(1),[name1] varchar(2),[amount1] int,[weight1] numeric(3,2))
insert [表1]
select 'A','N1',10,0.5 union all
select 'A','N8',6,2.1 union all
select 'B','N2',7,0.3 union all
select 'B','N3',4,1.1 union all
select 'B','N5',3,2.03 union all
select 'C','N8',5,1.02
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([code] varchar(1),[name2] varchar(2),[amount2] int,[weight2] numeric(3,2))
insert [表2]
select 'A','N6',2,0.4 union all
select 'A','N4',3,0.58 union all
select 'A','C2',5,1.2 union all
select 'B','D9',8,2.1
select * from [表1]
select * from [表2]
select code=isnull(a.code,b.code),name1,amount1,weight1,name2,amount2,weight2
from
(
select *,rn=row_number() over(partition by code order by amount1 desc)
from [表1]
) a
full join
(
select *,rn=row_number() over(partition by code order by amount2)
from [表2]
) b
on a.code=b.code and a.rn=b.rn
--测试结果:
/*
code name1 amount1 weight1 name2 amount2 weight2
---- ----- ----------- --------------------------------------- ----- ----------- ---------------------------------------
A N1 10 0.50 N6 2 0.40
A N8 6 2.10 N4 3 0.58
A NULL NULL NULL C2 5 1.20
B N2 7 0.30 D9 8 2.10
B N3 4 1.10 NULL NULL NULL
B N5 3 2.03 NULL NULL NULL
C N8 5 1.02 NULL NULL NULL
(7 row(s) affected)
*/
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([code] varchar(1),[name1] varchar(2),[amount1] int,[weight1] numeric(3,2))
insert [表1]
select 'A','N1',10,0.5 union all
select 'A','N8',6,2.1 union all
select 'B','N2',7,0.3 union all
select 'B','N3',4,1.1 union all
select 'B','N5',3,2.03 union all
select 'C','N8',5,1.02
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([code] varchar(1),[name2] varchar(2),[amount2] int,[weight2] numeric(3,2))
insert [表2]
select 'A','N6',2,0.4 union all
select 'A','N4',3,0.58 union all
select 'A','C2',5,1.2 union all
select 'B','D9',8,2.1
select * from [表1]
select * from [表2]
select code=isnull(a.code,b.code),name1,amount1,weight1,name2,amount2,weight2
from
(
select *,rn=(select count(*) from [表1] where code=t.code and amount1>t.amount1)
from [表1] t
) a
full join
(
select *,rn=(select count(*) from [表2] where code=t.code and amount2<t.amount2)
from [表2] t
) b
on a.code=b.code and a.rn=b.rn
order by code
--测试结果:
/*
code name1 amount1 weight1 name2 amount2 weight2
---- ----- ----------- --------------------------------------- ----- ----------- ---------------------------------------
A N1 10 0.50 N6 2 0.40
A N8 6 2.10 N4 3 0.58
A NULL NULL NULL C2 5 1.20
B N2 7 0.30 D9 8 2.10
B N3 4 1.10 NULL NULL NULL
B N5 3 2.03 NULL NULL NULL
C N8 5 1.02 NULL NULL NULL
(7 row(s) affected)
*/
表1
Code Name1 Amount1 Weight1
A N1 10 0.5
A N8 6 2.1
B N2 7 0.3
B N3 4 1.1
B N5 3 2.03
C N8 5 1.02
表2
Code Name2 Amount2 Weight2
A N6 2 0.4
A N4 3 0.58
A C2 5 1.2
B D9 8 2.1
连接后为
Code Name1 Amount1 Weight1 Name2 Amount2 Weight2
A N1 10 0.5 N6 2 0.4
A N8 6 2.1 N4 3 0.58
A <Null> <Null> <Null> C2 5 1.2
B N2 7 0.3 D9 8 2.1
B N3 4 1.1 <Null> <Null> <Null>
B N5 3 2.03 <Null> <Null> <Null>
C N8 5 1.02 <Null> <Null> <Null>
DECLARE @TA TABLE(Code Varchar(2),Name1 VARCHAR(2), Amount1 INT, Weight1 DECIMAL(10,2))
INSERT @TA
SELECT 'A','N1', 10, 0.5 UNION ALL
SELECT 'B','N2', 7, 0.3
DECLARE @TB TABLE( Code Varchar(2),Name2 VARCHAR(2),Amount2 INT,Weight2 DECIMAL(10,2))
INSERT @TB
SELECT'A', 'N6', 2, 0.4 UNION ALL
SELECT 'A','N4', 3, 0.58 UNION ALL
SELECT 'A','C2', 5, 1.2 UNION ALL
SELECT 'B','D9', 8, 2.1
select Code= case when ((select top 1 Name2 from @TB where code = b.code)= b.Name2) then a.code else '' end,
Name1 = case when ((select top 1 Name2 from @TB where code = b.code)= b.Name2) then a.Name1 else ''end,
Amount1 = case when ((select top 1 Name2 from @TB where code = b.code)= b.Name2) then a.Amount1 else ''end,
Weight1 = case when ((select top 1 Name2 from @TB where code = b.code)= b.Name2) then cast(a.Weight1 as varchar(10))else ''end ,
b.* from @TA as a right join @TB as b
on a.code=b.code
表1
Code Name1 Amount1 Weight1
A N1 10 0.5
A N8 6 2.1
B N2 7 0.3
B N3 4 1.1
B N5 3 2.03
C N8 5 1.02
表2
Code Name2 Amount2 Weight2
A N6 2 0.4
A N4 3 0.58
A C2 5 1.2
B D9 8 2.1
连接后为
Code Name1 Amount1 Weight1 Name2 Amount2 Weight2
A N1 10 0.5 N6 2 0.4
A N8 6 2.1 N4 3 0.58
A <Null> <Null> <Null> C2 5 1.2
B N2 7 0.3 D9 8 2.1
B N3 4 1.1 <Null> <Null> <Null>
B N5 3 2.03 <Null> <Null> <Null>
C N8 5 1.02 <Null> <Null> <Null>
连接后为
Code Name1 Amount1 Weight1 Name2 Amount2 Weight2
A N1 10 0.5 N6 2 0.4
A N8 6 2.1 N4 3 0.58
A <Null> <Null> <Null> C2 5 1.2
B N2 7 0.3 D9 8 2.1
B N3 4 1.1 <Null> <Null> <Null>
B N5 3 2.03 <Null> <Null> <Null>
C N8 5 1.02 <Null> <Null> <Null>
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([code] varchar(1),[name1] varchar(2),[amount1] int,[weight1] float)
insert [表1]
select 'a','n1',10,0.5 union all
select 'b','n2',7,0.3
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([code] varchar(1),[name2] varchar(2),[amount2] int,[weight2] float)
insert [表2]
select 'a','n6',2,0.4 union all
select 'a','n4',3,0.58 union all
select 'a','c2',5,1.2 union all
select 'b','d9',8,2.1
select * from [表1]
select * from [表2]
--测试结果:
/*
*/
select Code
,Name1=case rn when 0 then Name1 else null end
,Amount1=case rn when 0 then Amount1 else null end
,Weight1=case rn when 0 then Weight1 else null end
,Name2,Amount2,Weight2
from
(
select a.Code,a.Name1,a.Amount1,a.Weight1
,b.Name2,b.Amount2,b.Weight2
,rn=(select count(*) from 表2 where code=b.code and amount2<b.amount2)
from 表1 a join 表2 b
on a.code=b.code
) t
/*
Code Name1 Amount1 Weight1 Name2 Amount2 Weight2
---- ----- ----------- ---------------------- ----- ----------- ----------------------
a n1 10 0.5 n6 2 0.4
a NULL NULL NULL n4 3 0.58
a NULL NULL NULL c2 5 1.2
b n2 7 0.3 d9 8 2.1
(4 row(s) affected)
*/
if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([code] varchar(1),[name1] varchar(2),[amount1] int,[weight1] float)
insert [表1]
select 'a','n1',10,0.5 union all
select 'b','n2',7,0.3
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([code] varchar(1),[name2] varchar(2),[amount2] int,[weight2] float)
insert [表2]
select 'a','n6',2,0.4 union all
select 'a','n4',3,0.58 union all
select 'a','c2',5,1.2 union all
select 'b','d9',8,2.1
select * from [表1]
select * from [表2]
--测试结果:
/*
*/
select Code
,Name1=case rn when 1 then Name1 else null end
,Amount1=case rn when 1 then Amount1 else null end
,Weight1=case rn when 1 then Weight1 else null end
,Name2,Amount2,Weight2
from
(
select a.Code,a.Name1,a.Amount1,a.Weight1
,b.Name2,b.Amount2,b.Weight2
,rn=row_number() over(partition by b.code order by b.Amount2)
from 表1 a join 表2 b
on a.code=b.code
) t
/*
Code Name1 Amount1 Weight1 Name2 Amount2 Weight2
---- ----- ----------- ---------------------- ----- ----------- ----------------------
a n1 10 0.5 n6 2 0.4
a NULL NULL NULL n4 3 0.58
a NULL NULL NULL c2 5 1.2
b n2 7 0.3 d9 8 2.1
(4 row(s) affected)
*/
Code Name1 Amount1 Weight1 Name2 Amount2 Weight2
A N1 10 0.5 N6 2 0.4
A N4 3 0.58
A C2 5 1.2
B N2 7 0.3 D9 8 2.1
表1
Code Name1 Amount1 Weight1
A N1 10 0.5
B N2 7 0.3
表2
Code Name2 Amount2 Weight2
A N6 2 0.4
A N4 3 0.58
A C2 5 1.2
B D9 8 2.1
连接后为
Code Name1 Amount1 Weight1 Name2 Amount2 Weight2
A N1 10 0.5 N6 2 0.4
N4 3 0.58
C2 5 1.2
B N2 7 0.3 D9 8 2.1