27,579
社区成员
发帖
与我相关
我的任务
分享
select z.fno,isnull(qty_a,0) as qty_a,isnull(qty_b,0) as qty_b,isnull(qty_c,0) as qty_c from
(select distinct fno from t04a
union
select distinct fno from t04b
union
select distinct fno from t04c
) as z
full join
(select *,fqty as qty_a from t04a) as a on z.fno=a.fno
full join
(select *,fqty as qty_b from t04b) as b on z.fno=b.fno
full join
(select *,fqty as qty_c from t04c) as c on z.fno=c.fno
CREATE TABLE [T04a] (
[fno] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[fqty] [int] null
) ON [PRIMARY]
GO
CREATE TABLE [T04b] (
[fno] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[fqty] [int] null
) ON [PRIMARY]
GO
CREATE TABLE [T04c] (
[fno] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[fqty] [int] null
) ON [PRIMARY]
GO
insert into t04a select '204-035',1000
insert into t04a select '206-052',250
insert into t04b select '204-035',800
insert into t04b select '208-000',500
insert into t04c select '208-000',300
insert into t04c select '206-052',700
select z.fno,qty_a,qty_b,qty_c from
(select distinct fno from t04a
union
select distinct fno from t04b
union
select distinct fno from t04c
) as z
full join
(select *,fqty as qty_a from t04a) as a on z.fno=a.fno
full join
(select *,fqty as qty_b from t04b) as b on z.fno=b.fno
full join
(select *,fqty as qty_c from t04c) as c on z.fno=c.fno
用full join
或用
select
PARTNO,RESIN,GRADE,[USE],DIM,UNIT1,DIM2,[TYPE],UNIT,
[9月库存数]=sum(case when [Month]=9 then QTY else 0 end),
[10月库存数]=sum(case when [Month]=10 then QTY else 0 end),
[11库存数]=sum(case when [Month]=11 then QTY else 0 end)
from
(select *,[Month]=9 from A
union all
select * ,[Month]=10 from B
union all
select * ,[Month]=11 from C)T
group by PARTNO,RESIN,GRADE,[USE],DIM,UNIT1,DIM2,[TYPE],UNIT
--假设三表partno一一对应。
select a.* , b.QTY [10月库存数], c.QTY [11库存数]
from a,b,c
where a.PARTNO = b.PARTNO and a.partno = c.PARTNO
--如果不是一一对应,使用full join (其中里面的字段自己写完)
select isnull(m.partno , n.partno) partno , .... , isnull(m.[9月库存数],0) [9月库存数] , isnull(m.[10月库存数],0) [10月库存数] , isnull(n.qty , 0) [11月库存数]
from
(
select isnull(a.partno , b.partno) partno , ...., isnull(a.QTY,0) [9月库存数] , isnull(b.QTY,0) [10月库存数] from a full join b on a.partno = b.partno
) m
full join c n
on m.partno = n.partno
--假设三表partno一一对应。
select a.* , b.QTY [10月库存数], c.QTY [11库存数]
from a,b,c
where a.PARTNO = b.PARTNO and a.partno = c.PARTNO