表1:初期结余表
产品编号 数量
1         100
3          500
4           800
=================================
表2:本期入库表 
产品编号 数量
1         100
1         80
2         800
=============================
表3:本期出库表
产品编号 数量
2         100
3         100
3         200
================================
构造查询,得到如下的结果:产品编号 期初数量 入库数量 出库数量 结存数量
1          100          180         0          280
2          0          800         100          700
3          500          0         300          200
4          800          0         0          800

解决方案 »

  1.   

    select
       isnull(a.产品编号,b.产品编号) as 产品编号,
       isnull(a.数量,0) as 期初数量,
       isnull(b.数量,0) as 入库数量,
       isnull(a.数量,0)-isnull(b.数量,0) as 结存数量
    from
       初期结余表 a
    full join
       (select 产品编号,sum(数量) as 数量  from 本期入库表 group by  产品编号)b
    on
       a.产品编号=b.产品编号
    left join
       (select 产品编号,sum(数量) as 数量  from 本期出库表 group by  产品编号)c
    on
       b.产品编号=c.产品编号 
      

  2.   

    中文看着累 后面不想写了~select isnull(a.产品编号,b.产品编号) 产品编号,a.数量
     as  期初数量 , b.数量 as 入库数量 from 
    (select 产品编号, sum(数量
    ) 数量
     from  初期结余表
    group by  产品编号) aleft join 
    (select 产品编号 sum(数量
    ) 数量
    from  本期入库表 ) b
    on a.产品编号=b.产品编号
      

  3.   


    select a.产品编号,sum(isnull(期初数量,0)),sum(isnull(入库数量,0)),sum(isnull(出库数量,0)),sum(isnull(期初数量,0))+sum(isnull(入库数量,0))-sum(isnull(出库数量,0)) as 结存数量
    from 初期结余表 a full join 本期入库表 b on a.产品编号=b.产品编号 full join 本期出库表 c a.产品编号=c.产品编号 where a.产品编号 is not null  group by a.产品编号
      

  4.   

    select 产品编号,期初数量,sum(入库数量) as 入库数量,sum(出库数量) as 出库数量,期初数量+sum(入库数量)-sum(出库数量) as 结存数量 from 
    (
    select 
    (case when a.产品编号 is not null then a.产品编号
    when a.产品编号 is null and b.产品编号 is null then c.产品编号
      else b.产品编号 end) as 产品编号,
    isnull(a.数量,0) as 期初数量,
    isnull(b.数量,0) as 入库数量,
    isnull(c.数量,0) as 出库数量
    from 初期结余表 a 
    full join 本期入库表 b on a.产品编号=b.产品编号  
    full join 本期出库表 c on a.产品编号=c.产品编号
    ) d
    group by 产品编号,期初数量
    order by 产品编号
      

  5.   


    use tempdb;
    /*
    create table 初期结余
    (
    产品编号 int not null,
    数量 int not null
    );
    insert into 初期结余(产品编号,数量)
    values
    (1,100),
    (3,500),
    (4,800);create table 本期入库
    (
    产品编号 int not null,
    数量 int not null
    );
    insert into 本期入库(产品编号,数量)
    values
    (1,100),
    (1,80),
    (2,800);create table 本期出库
    (
    产品编号 int not null,
    数量 int not null
    );
    insert into 本期出库(产品编号,数量)
    values
    (2,100),
    (3,100),
    (3,200);
    */
    select 
    t.产品编号,
    MAX(t.期初数量) as 期初数量,
    SUM(t.入库数量) as 入库数量,
    SUM(t.出库数量) as 出库数量,
    (MAX(t.期初数量) + SUM(t.入库数量) - SUM(t.出库数量)) as [结存数量]
    from
    (
    select 
    case 
    when t1.[产品编号] is null then ISNULL(t2.[产品编号],t3.[产品编号]) 
    when t2.[产品编号] is null then ISNULL(t3.[产品编号],t1.[产品编号]) 
    when t3.[产品编号] is null then ISNULL(t1.[产品编号],t2.[产品编号]) 
    end as [产品编号],
    ISNULL(t1.数量,0) as [期初数量],
    ISNULL(t2.数量,0) as [入库数量],
    ISNULL(t3.数量,0) as [出库数量]
    from 初期结余 as t1
    full join 本期入库 as t2 on t1.产品编号 = t2.产品编号
    full join 本期出库 as t3 on t1.产品编号 = t3.产品编号
    ) as t
    group by t.产品编号;
      

  6.   

    select isnull(isnull(t1.产品编号,t2.产品编号),t3.产品编号) 产品编号,
           isnull(t1.数量,0) 期初数量,
           isnull(t2.数量,0) 入库数量,
           isnull(t3.数量,0) 出库数量,
           isnull(t1.数量,0) + isnull(t2.数量,0) - isnull(t3.数量,0) 结存数量
    from 初期结余表 t1 full join
    (select 产品编号 , sum(数量) 数量 from 本期入库表 group by 产品编号) t2 
    on t1.产品编号 = t2.产品编号 full join
    (select 产品编号 , sum(数量) 数量 from 本期出库表 group by 产品编号) t3
    on  isnull(t1.产品编号,t2.产品编号) = t3.产品编号
      

  7.   


    create table 初期结余表
    (
    产品编号 int,
    数量 int
    )
    insert into 初期结余表 select 1,100 
    union all select 2,500
    union all select 4,800
    create table 本期入库表
    (
    产品编号 int,
    数量 int
    )
    insert into 本期入库表 select 1,100 
    union all select 1,80
    union all select 2,800
    create table 本期出库表
    (
    产品编号 int,
    数量 int
    )
    insert into 本期出库表 select 2,100 
    union all select 3,100
    union all select 3,200
    select (case when a.产品编号 is not null then a.产品编号
    when a.产品编号 is null and b.产品编号 is null then c.产品编号
      else b.产品编号 end) as 产品编号
    ,max(isnull(a.数量,0)) as 期初数量
    ,sum(isnull(b.数量,0)) as 入库数量
    ,sum(isnull(c.数量,0)) as 出库数量
    ,max(isnull(a.数量,0))+sum(isnull(b.数量,0))-sum(isnull(c.数量,0)) as 结存数量
    from 初期结余表 a 
    full join 本期入库表 b 
    on a.产品编号=b.产品编号 
    full join 本期出库表 c on
    a.产品编号=c.产品编号 
    group by (case when a.产品编号 is not null then a.产品编号
    when a.产品编号 is null and b.产品编号 is null then c.产品编号
      else b.产品编号 end) 
    学习
      

  8.   


    create table #初期结余表
    (产品编号 int, 数量 int)
    insert #初期结余表
    select 1, 100 union all
    select 3 ,500 union all
    select 4 ,800create table #本期入库表 
    (产品编号 int, 数量 int)
    insert  #本期入库表 
    select 1, 100 union all
    select 1, 80 union all
    select 2, 800create table #本期出库表
    (产品编号 int, 数量 int)
    insert #本期出库表
    select 2, 100 union all
    select 3, 100 union all
    select 3, 200select numT.产品编号,                           --我自己的笨方法
           isnull(T1.数量,0) as 数量,
           isnull(t2.入库数量,0) as 入库数量,
           isnull(t3.出库数量,0) as 出库数量,
           isnull(T1.数量,0)+isnull(t2.入库数量,0)-isnull(t3.出库数量,0) as 结存数量       
     from
           (select 产品编号 from #初期结余表 union 
            select 产品编号 from #本期入库表 union 
            select 产品编号 from #本期出库表) as numT 
     Left join
            #初期结余表 as T1 on T1.产品编号=numT.产品编号
     Left join
           (select 产品编号,sum(数量) as 入库数量 from #本期入库表 group by 产品编号) as T2 on T2.产品编号=numT.产品编号
     Left join
           (select 产品编号,sum(数量) as 出库数量 from #本期出库表 group by 产品编号) as T3 on T3.产品编号=numT.产品编号select isnull(isnull(T1.产品编号,T2.产品编号),T3.产品编号)as 产品编号,   --效果一样,不过应该好些
           isnull(T1.数量,0) as 数量,
           sum(isnull(T2.数量,0)) as 入库数量,
           sum(isnull(T3.数量,0)) as 出库数量,
           isnull(T1.数量,0) + sum(isnull(T2.数量,0)) - sum(isnull(T3.数量,0)) as 结存数量            
     from  #初期结余表 as T1 
     full join
            #本期入库表 as T2 on T2.产品编号=T1.产品编号
     full join
            #本期出库表 as T3 on T3.产品编号=T1.产品编号
     group by isnull(isnull(T1.产品编号,T2.产品编号),T3.产品编号),isnull(T1.数量,0)