现在有表1中的数据,希望将月份信息提出来作为新表的列,新表中的数据为旧表里的报告提交时间。报告人:nvarchar(50)
报告月份:datetime
报告时间:datetime表1(原始数据表)
报告人 报告月份   报告提交时间
报告人1 2008.8.1 2008.8.8
报告人2 2008.8.1 2008.8.9
报告人3 2008.8.1 2008.8.1
报告人1 2008.7.1 2008.7.8
报告人2 2008.7.1 2008.7.9
报告人3 2008.7.1 2008.7.10表2(期望结果表)
报告人 2008.7         2008.8
报告人1 2008.7.8 2008.8.8
报告人2 2008.7.9 2008.8.9
报告人3 2008.7.10 2008.8.10

解决方案 »

  1.   

    declare @sql varchar(8000)
    set @sql=''select @sql=@sql+',['+convert(char(7),报告月份,120)+']=max(case 报告月份 when '''+convert(varchar(20),报告月份,120)+''' then 报告提交时间 end)'
    from (select distinct 报告月份 from 表1) tset @sql='select 报告人'+@sql+' from 表1 group by 报告人'exec(@sql)
      

  2.   

    create table tb(报告人 varchar(20) , 报告月份 datetime, 报告提交时间 datetime)
    insert into tb values('报告人1', '2008.8.1' ,'2008.8.8' )
    insert into tb values('报告人2', '2008.8.1' ,'2008.8.9' )
    insert into tb values('报告人3', '2008.8.1' ,'2008.8.1' )
    insert into tb values('报告人1', '2008.7.1' ,'2008.7.8' )
    insert into tb values('报告人2', '2008.7.1' ,'2008.7.9' )
    insert into tb values('报告人3', '2008.7.1' ,'2008.7.10')
    goselect 报告人,
    max(case convert(varchar(7),报告提交时间,120) when '2008-07' then 报告提交时间 else null end) '2008-07',
    max(case convert(varchar(7),报告提交时间,120) when '2008-08' then 报告提交时间 else null end) '2008-08'
    from tb
    group by 报告人drop table tb/*报告人                  2008-07                                                2008-08                                                
    -------------------- ------------------------------------------------------ ------------------------------------------------------ 
    报告人1                 2008-07-08 00:00:00.000                                2008-08-08 00:00:00.000
    报告人2                 2008-07-09 00:00:00.000                                2008-08-09 00:00:00.000
    报告人3                 2008-07-10 00:00:00.000                                2008-08-01 00:00:00.000(所影响的行数为 3 行)
    */
      

  3.   

    --> 测试数据: [表1]
    if object_id('[表1]') is not null drop table [表1]
    create table [表1] (报告人 varchar(7),报告月份 datetime,报告提交时间 datetime)
    insert into [表1]
    select '报告人1','2008.8.1','2008.8.8' union all
    select '报告人2','2008.8.1','2008.8.9' union all
    select '报告人3','2008.8.1','2008.8.1' union all
    select '报告人1','2008.7.1','2008.7.8' union all
    select '报告人2','2008.7.1','2008.7.9' union all
    select '报告人3','2008.7.1','2008.7.10'
    declare @sql varchar(8000)
    set @sql='select 报告人'
    select @sql=@sql+',['+报告月份+']=max(case convert(varchar(7),报告月份,102) when '''+报告月份+'''
    then 报告提交时间 else null end)' from (select distinct 报告月份=convert(varchar(7),报告月份,102) from [表1])a
    set @sql=@sql+' from 表1 group by 报告人'
    exec(@sql)
    --结果:
    报告人     2008.07                                           2008.08                
    ------- --------------------------- ------------------------------------ 
    报告人1    2008-07-08 00:00:00.000                                2008-08-08 00:00:00.000
    报告人2    2008-07-09 00:00:00.000                                2008-08-09 00:00:00.000
    报告人3    2008-07-10 00:00:00.000                                2008-08-01 00:00:00.000
      

  4.   

    decode 函数了,数据量小,可以用choose case 
      

  5.   

    create table 表1(报告人 varchar(8),报告月份 datetime,报告提交时间 datetime)
    insert into 表1 values('报告人1','2008.8.1','2008.8.8' )
    insert into 表1 values('报告人2','2008.8.1','2008.8.9' )
    insert into 表1 values('报告人3','2008.8.1','2008.8.1' )
    insert into 表1 values('报告人1','2008.7.1','2008.7.8' )
    insert into 表1 values('报告人2','2008.7.1','2008.7.9' )
    insert into 表1 values('报告人3','2008.7.1','2008.7.10') 
    godeclare @sql varchar(8000)
    set @sql=''select @sql=@sql+',['+convert(char(7),报告月份,120)+']=max(case 报告月份 when '''+convert(varchar(20),报告月份,120)+''' then 报告提交时间 end)'
    from (select distinct 报告月份 from 表1) tset @sql='select 报告人'+@sql+' from 表1 group by 报告人'exec(@sql)
    go/*
    报告人      2008-07                                                2008-08                                                
    -------- ------------------------------------------------------ ------------------------------------------------------ 
    报告人1     2008-07-08 00:00:00.000                                2008-08-08 00:00:00.000
    报告人2     2008-07-09 00:00:00.000                                2008-08-09 00:00:00.000
    报告人3     2008-07-10 00:00:00.000                                2008-08-01 00:00:00.000
    */drop table 表1
    go
      

  6.   

    create table tb(报告人 varchar(20) , 报告月份 datetime, 报告提交时间 datetime)
    insert into tb values('报告人1', '2008.8.1' ,'2008.8.8' )
    insert into tb values('报告人2', '2008.8.1' ,'2008.8.9' )
    insert into tb values('报告人3', '2008.8.1' ,'2008.8.1' )
    insert into tb values('报告人1', '2008.7.1' ,'2008.7.8' )
    insert into tb values('报告人2', '2008.7.1' ,'2008.7.9' )
    insert into tb values('报告人3', '2008.7.1' ,'2008.7.10')
    go
    --sql静态语句。固定月份为2008-07,2008-08
    select 报告人,
    max(case convert(varchar(7),报告提交时间,120) when '2008-07' then 报告提交时间 else null end) '2008-07',
    max(case convert(varchar(7),报告提交时间,120) when '2008-08' then 报告提交时间 else null end) '2008-08'
    from tb
    group by 报告人--SQL动态语句,月份不固定
    declare @sql varchar(8000)
    set @sql = 'select 报告人 '
    select @sql = @sql + ' , max(case convert(varchar(7),报告提交时间,120) when ''' + 月份 + ''' then 报告提交时间 else 0 end) [' + 月份 + ']'
    from (select distinct convert(varchar(7),报告提交时间,120) 月份  from tb) as a
    set @sql = @sql + ' from tb group by 报告人'
    exec(@sql) drop table tb/*报告人                  2008-07                                                2008-08                                                
    -------------------- ------------------------------------------------------ ------------------------------------------------------ 
    报告人1                 2008-07-08 00:00:00.000                                2008-08-08 00:00:00.000
    报告人2                 2008-07-09 00:00:00.000                                2008-08-09 00:00:00.000
    报告人3                 2008-07-10 00:00:00.000                                2008-08-01 00:00:00.000(所影响的行数为 3 行)
    */
      

  7.   

    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',max(case when convert(varchar(7),报告月份,102)='''+convert(varchar(7),报告月份,102)+''' then 报告提交时间 end) as ['+convert(varchar(7),报告月份,102)+']'
    from 原始数据表 
    group by convert(varchar(7),报告月份,102)exec('select 报告人'+@sql+' from 原始数据表 group by 报告人')
      

  8.   

    create table tb(报告人 nvarchar(50),报告月份 datetime,报告时间 datetime)
    insert into tb select '报告人1','2008.8.1','2008.8.8'
    insert into tb select '报告人2','2008.8.1','2008.8.9'
    insert into tb select '报告人3','2008.8.1','2008.8.1'
    insert into tb select '报告人1','2008.7.1','2008.7.8'
    insert into tb select '报告人2','2008.7.1','2008.7.9'
    insert into tb select '报告人3','2008.7.1','2008.7.10'declare @sql varchar(8000)
    select @sql=isnull(@sql+',','')+'max(case when convert(varchar(7),报告月份,120)='''+convert(varchar(7),报告月份,120)+''' then convert(varchar(10),报告时间,120) else '''' end) as ['+convert(varchar(7),报告月份,120)+']'
    from (select distinct convert(varchar(7),报告月份,120) as 报告月份 from tb)t
    exec('select 报告人,'+@sql+' from tb group by 报告人')报告人 2008-07 2008-08
    报告人1 2008-07-08 2008-08-08
    报告人2 2008-07-09 2008-08-09
    报告人3 2008-07-10 2008-08-01
      

  9.   

    CREATE TABLE tb
    (报告人 NVARCHAR(50),报告月份 DATETIME,报告时间 DATETIME )
    INSERT tb SELECT '报告人1','2008.8.1','2008.8.8'UNION ALL SELECT 
    '报告人2','2008.8.1','2008.8.9'UNION ALL SELECT 
    '报告人3','2008.8.1','2008.8.1'UNION ALL SELECT
    '报告人1','2008.7.1','2008.7.8' UNION ALL SELECT
    '报告人2','2008.7.1','2008.7.9' UNION ALL SELECT
    '报告人3','2008.7.1','2008.7.10'
    declare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+',['+convert(char(7),报告月份,120)+']=max(case 报告月份 when '''+convert(varchar(20),报告月份,120)+''' then 报告时间 end)'
    from (select distinct 报告月份 from tb) t
    set @sql='select 报告人'+@sql+' from tb group by 报告人'
    exec(@sql)
    报告人                                                2008-07                                                2008-08                                                
    -------------------------------------------------- ------------------------------------------------------ ------------------------------------------------------ 
    报告人1                                               2008-07-08 00:00:00.000                                2008-08-08 00:00:00.000
    报告人2                                               2008-07-09 00:00:00.000                                2008-08-09 00:00:00.000
    报告人3                                               2008-07-10 00:00:00.000                                2008-08-01 00:00:00.000
      

  10.   

    declare @sql varchar(8000)
    select @sql=isnull(@sql+',','')+'max(case when convert(varchar(7),报告月份,120)='''+convert(varchar(7),报告月份,120)+''' then replace(convert(varchar(10),报告时间,120),''-'',''.'') else '''' end) as ['+replace(convert(varchar(7),报告月份,120),'-','.')+']'
    from (select distinct convert(varchar(7),报告月份,120) as 报告月份 from tb)t
    exec('select 报告人,'+@sql+' from tb group by 报告人')报告人 2008.07 2008.08
    报告人1 2008.07.08 2008.08.08
    报告人2 2008.07.09 2008.08.09
    报告人3 2008.07.10 2008.08.01