数据库表:
材料编号    材料名称   申请数量    申请日期(datetime)要求生成的数据:
材料编号  材料名称   一月份的申请数量  二月份的申请数量   合计数量实在是不知道怎么写,各位高手帮帮忙,谢谢了!

解决方案 »

  1.   

    create table ta(材料编号 varchar(5),材料名称 varchar(10),申请数量 int,申请日期 datetime)
    insert ta
    select '0001','木材',50,'2006-01-01 00:00:00' union all
    select '0002','钢材',100,'2006-01-02 00:00:00' union all
    select '0001','木材',200,'2006-01-03 00:00:00' union all
    select '0001','木材',50,'2006-02-01 00:00:00' union all
    select '0002','钢材',100,'2006-02-02 00:00:00' union all
    select '0001','木头',200,'2006-02-03 00:00:00' 动态,静态都有了,楼主可以结贴了
    declare @sql varchar(4000)
    set @sql=''
    select @sql=@sql+',[2006年第'+convert(varchar,月份)+'月]=sum(case month(申请日期) when '
    +convert(varchar,月份)+' then 申请数量 else 0 end)'
    from (select 月份=month(申请日期) from ta where year(申请日期)=2006 group by month(申请日期))a
    --print @sql
    select @sql='select 材料编号,材料名称 '+@sql+'from ta where year(申请日期)=2006 group by 材料编号,材料名称'
    exec(@sql)
    --动态
    select 材料编号,材料名称
    ,[2006年第1月]=sum(case month(申请日期) when 1 then 申请数量 else 0 end),
    [2006年第2月]=sum(case month(申请日期) when 2 then 申请数量 else 0 end)
    from ta
    where year(申请日期)=2006
    group by 材料编号,材料名称
    --静态
    材料编号  材料名称       2006年第1月    2006年第2月    
    ----- ---------- ----------- ----------- 
    0002  钢材         100         100
    0001  木材         250         50
    0001  木头         0           200
      

  2.   

    create table ta(材料编号 varchar(5),材料名称 varchar(10),申请数量 int,申请日期 datetime)
    insert ta
    select '0001','木材',50,'2006-01-01 00:00:00' union all
    select '0002','钢材',100,'2006-01-02 00:00:00' union all
    select '0001','木材',200,'2006-01-03 00:00:00' union all
    select '0001','木材',50,'2006-02-01 00:00:00' union all
    select '0002','钢材',100,'2006-02-02 00:00:00' union all
    select '0003','木头',200,'2006-02-03 00:00:00' 
    select  * from taselect 材料编号,材料名称,
    sum(case month(申请日期) when 1 then 申请数量 else 0 end) as [1月份统计],
    sum(case month(申请日期) when 2 then 申请数量 else 0 end) as [2月份统计],
    sum(申请数量) as 材料统计 
    from ta 
    group by 材料编号,材料名称
    order by 材料编号,材料名称drop table  ta
      

  3.   

    --借用上面兄弟的数据
    create table ta(材料编号 varchar(5),材料名称 varchar(10),申请数量 int,申请日期 datetime)insert ta
    select '0001','木材',50,'2006-01-01 00:00:00' union all
    select '0002','钢材',100,'2006-01-02 00:00:00' union all
    select '0001','木材',200,'2006-01-03 00:00:00' union allselect '0001','木材',50,'2006-02-01 00:00:00' union all
    select '0002','钢材',100,'2006-02-02 00:00:00' union all
    select '0001','木头',200,'2006-02-03 00:00:00' 
    --SQL语名
    select 材料编号, 材料名称, 
    一月份的申请数量=sum(case when month(申请日期)=1 then 申请数量 end),
    二月份的申请数量=sum(case when month(申请日期)=2 then 申请数量 end),
    合计数量=sum(申请数量)
    from ta
    group by 材料编号, 材料名称--结果
    材料编号  材料名称       一月份的申请数量    二月份的申请数量    合计数量        
    ----- ---------- ----------- ----------- ----------- 
    0002  钢材         100         100         200
    0001  木材         250         50          300
    0001  木头         NULL        200         200(3 row(s) affected)
      

  4.   

    楼上的 你好像就比我少个order by
    另:
    '0001','木材'
    '0001','木头'
    这两行是不是有问题
      

  5.   

    语句(只需要把"表"改为对应的就可以了):
    select *(select 材料编号    材料名称  sum( 申请数量)as 合计数量 from 表 group by 材料编号    材料名称)as AA inner join (select * from (select 材料编号    材料名称  sum( 申请数量)as 一月份的申请数量 from 表 where 申请日期(datetime) between '2006-01-01 00:00:00' and '2006-02-01 00:00:00' group by 材料编号    材料名称) as A inner join (select 材料编号    材料名称  sum( 申请数量)as 二月份的申请数量 from 表 where 申请日期(datetime) between '2006-02-01 00:00:00' and '2006-03-01 00:00:00' group by 材料编号    材料名称) as B where A.材料编号=B.材料编号 and A.材料名称=B.材料名称)as BB where AA.材料编号=BB.材料编号 and AA.材料名称=BB.材料名称
      

  6.   

    楼主,在后面加合计就行了
    create table ta(材料编号 varchar(5),材料名称 varchar(10),申请数量 int,申请日期 datetime)
    insert ta
    select '0001','木材',50,'2006-01-01 00:00:00' union all
    select '0002','钢材',100,'2006-01-02 00:00:00' union all
    select '0001','木材',200,'2006-01-03 00:00:00' union all
    select '0001','木材',50,'2006-02-01 00:00:00' union all
    select '0002','钢材',100,'2006-02-02 00:00:00' union all
    select '0001','木头',200,'2006-02-03 00:00:00' 
    select 材料编号,材料名称
    ,[2006年第1月]=sum(case month(申请日期) when 1 then 申请数量 else 0 end),
    [2006年第2月]=sum(case month(申请日期) when 2 then 申请数量 else 0 end),
    [合计]=sum(申请数量)
    from ta
    where year(申请日期)=2006
    group by 材料编号,材料名称材料编号  材料名称       2006年第1月    2006年第2月    合计          
    ----- ---------- ----------- ----------- ----------- 
    0002  钢材         100         100         200
    0001  木材         250         50          300
    0001  木头         0           200         200(所影响的行数为 3 行)
      

  7.   

    这样类似的很多的!学会用case when就很简单!