Table1:物料清单
id   物料号  物料名称
1    A            打发打发
2    B            哦哦
3    C           如果法官
4    D           全球啊啊
5    E           白色通过Table2:期初库存
id   物料号  数量
1    A            100
2    B            100
3    C           100
4    D           100
5    E           100Table2:入库表
id   物料id   入库数量   入库日期
1    1            100            2014-07-05
2    2            150           2014-07-05
3    3            200           2014-07-05
4    1            100            2014-07-04
5    2            150           2014-07-04
6    3            200           2014-07-04Table3:出库表
id   物料id   出库数量  出库日期
1    3            50               2014-07-05
2    4            20               2014-07-05
3    5            10               2014-07-05
4    3            50               2014-07-06
5    4            20               2014-07-06
6    1            10               2014-07-06希望得到的结果
物料号  物料名称   期初库存   入库    出库   库存
A            打发打发   100           200        10     290
 B            哦哦          100           300        0       400
C           如果法官    100           400        100  400
 D           全球啊啊  100            0            40     60
E           白色通过   100            0            10     90

解决方案 »

  1.   

    select 物料号,物料名称,select ... as 期初库存, ... from tabl11
    大概是这个样子,其实就是每个字段分别从一个表中统计得到
      

  2.   


    use tempdbcreate table material(id int not null,
    materialNO varchar(30),materialName varchar(100),
    constraint pk_material_id primary key (id))create table history(id int not null,
    materialNO varchar(30),qty int,
    constraint pk_history_id primary key (id))create table ruku(id int not null,
    materialID int,ruku int ,
    ruDate date,constraint pk_ruku_id primary key (id))create table chuku(id int not null,
    materialID int,chuku int,
    chukuDate date,
    constraint pk_chuku_id primary key (id))
    insert into material
    select 1,    'A',            '打发打发' union all
    select 2,    'B',            '哦哦' union all
    select 3,    'C',           '如果法官' union all
    select 4,    'D',           '全球啊啊' union all
    select 5,    'E',           '白色通过'
    insert into history
    select 1,    'A',            100  union all
    select 2,    'B',            100  union all
    select 3,    'C',           100  union all
    select 4,    'D',           100  union all
    select 5,    'E',           100
    insert into ruku
    select 1,    1,            100,            '2014-07-05'   union all
    select 2,    2,            150,           '2014-07-05'  union all
    select 3,    3,            200,           '2014-07-05'  union all
    select 4,    1,            100,            '2014-07-04'  union all
    select 5,    2,            150,           '2014-07-04'  union all
    select 6,    3,            200,           '2014-07-04'
    insert into chuku
    select 1,    3,            50,               '2014-07-05'  union all
    select 2,    4,            20,               '2014-07-05'  union all
    select 3,    5,            10,               '2014-07-05'  union all
    select 4,    3,            50,               '2014-07-06'  union all
    select 5,    4,            20,              '2014-07-06'  union all
    select 6,    1,            10,               '2014-07-06' select a.materialno,b.materialName,a.qty,isnull(c.ruku,0) ruku,isnull(d.chuku,0) chuku,
     stock=a.qty+isnull(c.ruku,0)-isnull(d.chuku,0)
     from history a
     inner join material b on a.materialno=b.materialno
     left join (select materialID,sum(ruku) ruku from ruku 
     group by materialid) c on b.id=c.materialid
     left join (select materialID,sum(chuku) chuku from chuku
     group by materialID ) d on b.id=d.materialID