要生成这样的报表:
根据有进货表、库存表、商品档案表。
得出:
商品编号,1仓进货数量,1仓进货次数,1仓库存,........ n仓进货数量,n仓进货次数,n仓库存
怎么实现呀,请给指教一下

解决方案 »

  1.   

    参考
    http://blog.csdn.net/jinjazz/archive/2007/12/11/1928746.aspx--建立测试环境
    set nocount on
    create table test(model varchar(20),date int ,qty int)
    insert into test select 'a','8','10'
    insert into test select 'a','10','50'
    insert into test select 'b','8','100'
    insert into test select 'b','9','200'
    insert into test select 'b','10','100'
    insert into test select 'c','10','200'
    insert into test select 'd','10','300'
    insert into test select 'e','11','250'
    insert into test select 'e','12','100'
    insert into test select 'f','12','150'
    go
    --测试declare @sql varchar(8000)
    set @sql='select model,'
     select @sql=@sql+'sum(case when date='''+cast(date as varchar(10))+''' then qty else 0 end)['+cast(date as varchar(10))+'],'
    from (select distinct top 100 percent  date
     from test order by date)aset @sql =left(@sql,len(@sql)-1)+' from test group by model'exec(@sql) --删除测试环境
    drop table test
     set nocount off/**//*
    model                8           9           10          11          12
    -------------------- ----------- ----------- ----------- ----------- -----------
    a                    10          0           50          0           0
    b                    100         200         100         0           0
    c                    0           0           200         0           0
    d                    0           0           300         0           0
    e                    0           0           0           250         100
    f                    0           0           0           0           150
    */
      

  2.   

    举例:
    有三个表分别为X1(商品档案表),X2(仓库表),x3(进货表)
    数据以下:/*商品档案表(x1)
    商品编号 商品名称
    A1 电视机
    A2 洗衣机
    A3 电饭煲
    A4 空调仓库表(X2)
    商品编号 仓库
    A1 100
    A2 200
    A3 300
    A4 400进货表(x3)
    商品编号 进货数量
    A1 50
    A1 20
    A1 30
    A2 100
    A2 100
    A3 300
    A4 80
    A4 80
    A4 100
    A4 40
    A4 100
    */
    要求:得出各个商品进货的次数及仓库量select c. 商品名称, b.*
      from (select 商品编号, 进货次数 = sum(z_count), 仓存量 = sum(num)
              from (select 商品编号, z_count = 0, num = 0
                      from x1
                    union all
                    select 商品编号, z_count = count(1), num = 0
                      from x3
                     group by 商品编号
                    union all
                    select 商品编号, z_count = 0, num = 仓存 from x2) a
             group by 商品编号) b,
           x1 c
     where b. 商品编号 = c. 商品编号/*
    结果:
    商品名称 商品编号 进货次数 仓库量
    电视机 A1 3 100
    洗衣机 A2 2 200
    电饭煲 A3 1 300
    空调 A4 5 400*/
      

  3.   

    就用LEFT JOIN关联不就出来了吗