数据表
1 名称  日期      数量
  a     2000-1-1  10
  a     2000-1-1  10
  a     2000-1-4  10
  a     2000-1-5  10
  a     2000-2-1  20
  a     2000-2-2  10
  a     2000-2-4  10
  a     2000-2-5  10
b     2000-1-1  10
b     2000-2-1  10
b     2000-2-5  10
c     2000-1-1  10
c     2000-1-4  10
c     2000-2-1  10
c     2000-2-5  10
求出结果名称  1-1 1-4  1-5   1月小计   2-1  2-2   2-4 2-5 2月小计   合计
a     20  10   10   40    20   10    10  10 50  90
b     10  0    0     10    10   0      0   10 20  30
c     10  10   0     20    10   0     0   10        20        40
求行转列在按月份求小计!我求动态sql语句,而且效率要求高。谢谢。

解决方案 »

  1.   

    测试环境:
    ------------------
    create table #temp
    (名称 varchar(50),
    日期 varchar(50),
    数量 int
    )
    insert into #temp
    select 'a','2000-1-1','10' union all select 'a','2000-1-1','10' union all select 'a','2000-1-4','10' union all select 'a','2000-1-5','10' union all select 'a','2000-2-1','20' union all select 'a','2000-2-2','10' union all select 'a','2000-2-4','10' union all select 'a','2000-2-5','10' union all select 'b','2000-1-1','10' union all select 'b','2000-2-1','10' union all select 'b','2000-2-5','10' union all select 'c','2000-1-1','10' union all select 'c','2000-1-4','10' union all select 'c','2000-2-1','10' union all select 'c','2000-2-5','10'
    select * from #tempsql语句:
    ---------------------
    select distinct t.名称,
    sum(case 日期 when '2000-1-1' then t.sum1 end) [1-1],
    sum(case 日期 when '2000-1-4' then t.sum1 end) [1-4],
    sum(case 日期 when '2000-1-5' then t.sum1 end) [1-5],
    sum(case  when 日期 like '2000-1%' then t.sum1 end) 一月小计,
    sum(case 日期 when '2000-2-1' then t.sum1 end) [2-1],
    sum(case 日期 when '2000-2-2' then t.sum1 end) [2-2],
    sum(case 日期 when '2000-2-4' then t.sum1 end) [2-4],
    sum(case 日期 when '2000-2-5' then t.sum1 end) [2-5],
    sum(case  when 日期 like '2000-2%' then t.sum1 end) 二月小计,
    sum(t.sum1) 合计
    from
    (select 名称,日期,sum(数量) sum1 from #temp group by 名称,日期)t
    group by t.名称
      

  2.   

    用这个吧,把null都替换成0显示:select distinct t.名称,
    isnull(sum(case 日期 when '2000-1-1' then t.sum1 end),0) [1-1],
    isnull(sum(case 日期 when '2000-1-4' then t.sum1 end),0) [1-4],
    isnull(sum(case 日期 when '2000-1-5' then t.sum1 end),0) [1-5],
    isnull(sum(case  when 日期 like '2000-1%' then t.sum1 end),0) 一月小计,
    isnull(sum(case 日期 when '2000-2-1' then t.sum1 end),0) [2-1],
    isnull(sum(case 日期 when '2000-2-2' then t.sum1 end),0) [2-2],
    isnull(sum(case 日期 when '2000-2-4' then t.sum1 end),0) [2-4],
    isnull(sum(case 日期 when '2000-2-5' then t.sum1 end),0)[2-5],
    isnull(sum(case  when 日期 like '2000-2%' then t.sum1 end),0) 二月小计,
    sum(t.sum1) 合计
    from
    (select 名称,日期,sum(数量) sum1 from #temp group by 名称,日期)t
    group by t.名称-----------------------------名称  1-1 1-4  1-5  1月小计   2-1  2-2   2-4 2-5 2月小计   合计
    a     20  10   10   40        20   10     10  10   50      90
    b     10  0    0    10         10   0     0   10   20      30
    c     10  10   0    20          10   0     0  10   20      40
      

  3.   

    thanks,非常感谢!
    sql语句能不能改成动态的!日期是datetime类型的。
      

  4.   

    改成datetime建表,不过查询语句不用变,效果一样。
    动态的一会有空的话再尝试写一下。
    create table #temp
    (名称 varchar(50),
    日期 datetime,
    数量 int
    )
    insert into #temp
    select 'a','2000-1-1','10' union all select 'a','2000-1-1','10' union all select 'a','2000-1-4','10' union all select 'a','2000-1-5','10' union all select 'a','2000-2-1','20' union all select 'a','2000-2-2','10' union all select 'a','2000-2-4','10' union all select 'a','2000-2-5','10' union all select 'b','2000-1-1','10' union all select 'b','2000-2-1','10' union all select 'b','2000-2-5','10' union all select 'c','2000-1-1','10' union all select 'c','2000-1-4','10' union all select 'c','2000-2-1','10' union all select 'c','2000-2-5','10'
    select * from #temp
      

  5.   

    declare @sql varchar(8000)set @sql=' select 名称,总计=sum(数量) 'select @sql=@sql+',['+rtrim(日期)+']=sum(case 日期when '''+rtrim(日期)+''' then 数量else 0 end)'
     from #temp group by 日期order by 日期
    set @sql=@sql+' from #temp group by 名称'
    print @sql
    exec(@sql)这就是每个月的小计我不会写。帮帮忙。
      

  6.   

    看看这样可以么declare @sql varchar(8000)
    set @sql=''
    set @sql=' select distinct t.名称,总计=sum(t.sum1), '
    set @sql=@sql+' 一月小计=sum(case  when datepart(mm,日期)=''1'' then t.sum1 else 0 end),'
    set @sql=@sql+' 二月小计=sum(case  when datepart(mm,日期)=''2'' then t.sum1 else 0 end),'select @sql=@sql+'['+convert(varchar,日期,120)+']=sum(case 日期 when '''+convert(varchar,日期,120)+''' then t.sum1 else 0 end),'
     from #temp group by 日期 order by 日期
    set @sql=left(@sql,len(@sql)-1)+' from (select 名称,日期,sum(数量) sum1 from #temp group by 名称,日期)t group by t.名称'
    print @sql
    exec(@sql)
      

  7.   

    还想继续问!
    输出结果:
    名称  1-1 1-4  1-5  1月小计   2-1  2-2   2-4 2-5 2月小计   合计
    a     20  10   10    0  40    20   10    10  10  50        90
    b     10  0    0     0  10    10   0     0   10  20        30
    c     10  10   0     0  20    10   0     0   10  20        40
    合计  40  20   10    0  70    40   10    10  10  90        160