求一条SQL语句

表一 表2
id inqty id outqty
1 10 1 5
1 40 1 10
2 20 1 20
2 10 2 10
2 30 2 5

如何通过sql语句得到如下结果:
id inqty outqty
1 10 5
1 40 10
1 20
2 20 10
2 10 5
2 30

作者: syszj   发布时间: 2011-06-16

没看出什么规律来.

作者: xuam   发布时间: 2011-06-16

SQL code
select isnull(a.id,b.id) as id,isnull(ltrim(a.inqty),'') as inqty,
isnull(ltrim(b.outqty),'') as outqty
from a
full join b on a.id=b.id

作者: FlySQL   发布时间: 2011-06-16

SQL code
if object_id('[a]') is not null drop table [a]
go
create table [a]([id] int,[inqty] int)
insert [a]
select 1,10 union all
select 1,40 union all
select 2,20 union all
select 2,10 union all
select 2,30
go
if object_id('[b]') is not null drop table [b]
go
create table [b]([id] int,[outqty] int)
insert [b]
select 1,5 union all
select 1,10 union all
select 1,20 union all
select 2,10 union all
select 2,5
go

select isnull(a.id,b.id) as id,isnull(ltrim(a.inqty),'') as inqty,
isnull(ltrim(b.outqty),'') as outqty
from (select rn=row_number() over(order by getdate()),* from a) a
full join (select rn=row_number() over(order by getdate()),* from b) b on a.id=b.id and a.rn=b.rn

/**
id          inqty        outqty
----------- ------------ ------------
1           10           5
1           40           10
1                        20
2           10           10
2           30           5
2           20           

(6 行受影响)
**/

作者: FlySQL   发布时间: 2011-06-16