表1
部门名  年周     发注数  比率
001     20        1       10%
001     30        4       10%
001     40        5       10%
002     20        1       20%
002     40        1       20%
转化成表B
部门名   区分    20   30   40  统计001      发注数  1     4    5   10
         比率    10%   10%  10% 10%002      发注数  1     0    1    2
         比率    20%   -  20%  20%
谢谢,拜托了

解决方案 »

  1.   

    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',sum(case 年周 when '+cast(年周 as varchar)+' then 发注数 else 0 end) as ['+cast(年周 as varchar)+']',@sql1=@sql1+',max(case 年周 when '+cast(年周 as varchar)+' then 比率 end) as ['+cast(年周 as varchar)+']'
    from 表1
    group by 年周exec('select 部门名,''发注数'' as 区分'+@sql+',sum(发注数) as 统计 from 表1 group by 部门名 union all select 部门名,''比率'' as 区分'+@sql1+',max(比率) as 统计 from 表1 group by 部门名 order by 1,2')
      

  2.   

    create table 表1(部门名 varchar(10),年周 varchar(10),发注数 int,比率 varchar(10))
    insert 表1
    select '001','20',1,'10%'
    union select '001','30',4,'10%'
    union select '001','40',5,'10%'
    union select '002','20',1,'20%'
    union select '002','40',1,'20%'
    declare @sql varchar(8000),@sql1 varchar(8000)
    select @sql='select * from (select 部门名,''发注数'' as 区分',@sql1=',cast(sum(发注数) as varchar) as 统计 from 表1 group by 部门名 union all select 部门名,''比率'' as 区分'
    select @sql=@sql
    +',cast(sum(case 年周 when '+cast(年周 as varchar)+' then 发注数 else 0 end) as varchar) as ['+cast(年周 as varchar)
    +']',@sql1=@sql1+',isnull(max(case 年周 when '+cast(年周 as varchar)+' then 比率 end),''-'') as ['+cast(年周 as varchar)+']'
    from 表1 group by 年周exec(@sql+@sql1+',max(比率) as 统计 from 表1 group by 部门名 ) t order by 部门名')
      

  3.   

    create table t(部門名 varchar(10), 年周 int,    發註數 int,  比率 int)
    insert into tselect '001',     20,        1,       10 union all
    select '001',     30,        4,       10 union all
    select '001',     40,        5,       10 union all
    select '002',     20,        1,       20 union all
    select '002',     40,        1,       20
    select 部門名,'發註數'as '區分', sum(case when 年周=20 then 發註數 else 0 end)as '20',
                                  sum(case when 年周=30 then 發註數 else 0 end)as  '30',
                                  sum(case when 年周=40 then 發註數 else 0 end)as  '40',
                                  sum(發註數)as 統計
    from t
    group by 部門名
    union 
    select 部門名,'比率'as '區分',max(case when 年周=20 then 比率 else '-' end)as 比率1,
                                max(case when 年周=30 then 比率 else '-' end)as 比率2,
                                max(case when 年周=40 then 比率 else '-' end)as 比率3,
                                max(case when 年周=20 then 比率 else '-' end)as 比率4
    from t
    group by 部門名
    order by 部門名,區分 desc部門名        區分     20          30          40          統計          
    ---------- ------ ----------- ----------- ----------- ----------- 
    001        發註數    1           4           5           10
    001        比率     10          10          10          10
    002        發註數    1           0           1           2
    002        比率     20          0           20          20(4 row(s) affected)
      

  4.   

    select * into #t from 
    (
    select 部門名,'發註數'as '區分', sum(case when 年周=20 then 發註數 else 0 end)as '二十',
                                  sum(case when 年周=30 then 發註數 else 0 end)as  '三十',
                                  sum(case when 年周=40 then 發註數 else 0 end)as  '四十',
                                  sum(發註數)as 統計
    from t
    group by 部門名
    union 
    select 部門名,'比率'as '區分',max(case when 年周=20 then 比率 else '-' end)as 比率1,
                                max(case when 年周=30 then 比率 else '-' end)as 比率2,
                                max(case when 年周=40 then 比率 else '-' end)as 比率3,
                                max(case when 年周=20 then 比率 else '-' end)as 比率4
    from t
    group by 部門名)aorder by 部門名,區分 descselect * from #t
    union
    select '統計','發註數',sum(case when 區分='發註數' then 二十 else 0 end),
           sum(case when 區分='發註數' then 三十 else 0 end),
           sum(case when 區分='發註數' then 四十 else 0 end),
           sum(case when 區分='發註數' then 統計 else 0 end)from #t
    部門名        區分     二十          三十          四十          統計          
    ---------- ------ ----------- ----------- ----------- ----------- 
    001        比率     10          10          10          10
    001        發註數    1           4           5           10
    002        比率     20          0           20          20
    002        發註數    1           0           1           2
    統計         發註數    2           4           6           12(5 row(s) affected)
      

  5.   

    给一段代码你参考一下:Create table test (name char(10),km char(10),cj int)
    go
    insert test values('张三','语文',80)
    insert test values('张三','数学',86)
    insert test values('张三','英语',75)
    insert test values('李四','语文',78)
    insert test values('李四','数学',85)
    insert test values('李四','英语',78)/*想变成姓名   语文   数学   英语
    张三   80     86     75
    李四   78     85     78
    */declare @sql varchar(8000)
    set @sql = 'select name'
    select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']'
     from (select distinct km from test) as a
    select @sql = @sql+' from test group by name'
    exec(@sql)