有如下表
员工编号     日期      薪水
 001          0501     4000
 002          0501     5000
 ...          ...      ...
 001          0502      5650 
 002          0502      3450
 003          0502      2000
 ...           ...     .... 001           0606      10000现想要
员工编号      05年1月薪水    05年2月薪水  ........         06年6月薪水
001            4000           5650                          10000
...            ....              ...                           ...

解决方案 »

  1.   

    樓主搜一下關鍵字"行轉列",csdn上很多例子的
      

  2.   

    搜索行转列。
    1: 列转为行:
    eg1:
    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)drop table test eg2:
    有表A,
     id pid
     1   1
     1   2
     1   3
     2   1
     2   2
     3   1
    如何化成表B:
     id pid
      1  1,2,3
      2  1,2
      3  1
    或者是从表B变成A(不要用游标)
    以前有相似的列子,现在找不到了,帮帮忙!
    --1.创建一个合并的函数
    create function fmerg(@id int)
    returns varchar(8000)
    as
    begin
    declare @str varchar(8000)
    set @str=''
    select @str=@str+','+cast(pid as varchar) from 表A where id=@id
    set @str=right(@str,len(@str)-1)
    return(@str)
    End
    go--调用自定义函数得到结果
    select distinct id,dbo.fmerg(id) from 表A
      

  3.   

    create table salary
    (
    employeeid char(3),
    date char(4),
    salary int
    )
    insert into salary
    select '001','0501',4000 union all
    select '002','0501',5000 union all
    select '001','0502',5650 union all
    select '002','0502',3450 union all
    select '003','0502',2450 union all
    select '001','0606',10000declare @sql varchar(8000)
    select @sql=''
    select @sql=@sql+',sum(case when date='''+date+''' then salary else 0 end) as ['+left(date,2)+'年'+right(date,2)+'月]' from salary group by date
    select @sql='select employeeid'+@sql+' from salary group by employeeid'
    exec (@sql)
    --print @sql
    go
    drop table salary/*
    employeeid 05年01月      05年02月      06年06月      
    ---------- ----------- ----------- ----------- 
    001        4000        5650        10000
    002        5000        3450        0
    003        0           2450        0
    */
      

  4.   

    --SQL SERVER 2000
    DECLARE
      @S VARCHAR(2000)SET @S = ''SELECT @S=@S+',['+日期+']=SUM(CASE WHEN 日期='''+日期+''' THEN 薪水 ELSE 0 END)'
    FROM 表GROUP BY 日期SET @S = 'SELECT 员工编号,' + STUFF(@S,1,1,'') + ' FROM 表 GROUP BY 员工编号'EXEC( @S)
      

  5.   

    --SQL SERVER 2005
    SELECT * FROM #PIVOT(
     SUM(薪水)FOR 日期 IN ([0501],[0502]..[0606])) AS PIT
      

  6.   

    --沒測試
    declare @sql varchar(8000)
    select @sql=''
    select @sql=@sql+',sum(case when 日期='''+rtrim(日期)+''' then 薪水 else 0 end) as ['+left(日期,2)+'年'+right(日期,2)+'月薪水]' from T group by 日期 
    select @sql='select 員工編號' +@sql+' from T group by 員工編號'
    exec(@sql)