有这样的一个表:
项目 站名 值  时间
101   1   0.01  5:00
102   1   0.02  5:00
103   1   0.03  5:00
106   1   0.05  5:00
101   2   0.001  5:00
102   2   0.002  5:00
103   2   0.003  5:00
106   2   0.005  5:00我想 转换成这样的形式 并且写入 这张新的表
站名  101   102    103    106   时间
1     0.01  0.02   0.03   0.05  5:00
2     0.001 0.002  0.003  0.005 5:00

解决方案 »

  1.   

    测试:
    create table 表(项目 int, 站名 int,值 varchar(10), 时间 varchar(4))
    insert 表 select 101,   1   ,'0.01','5:00'
    union all select 102 ,  1   ,'0.02','5:00'
    union all select 103  , 1   ,'0.03','5:00'
    union all select 106   ,1   ,'0.05','5:00'
    union all select 101   ,2   ,'0.001','5:00'
    union all select 102  , 2   ,'0.002','5:00'
    union all select 103 ,  2   ,'0.003','5:00'
    union all select 106,   2   ,'0.005','5:00'declare @sql varchar(8000)
    set @sql = 'select 站名'
    select @sql = @sql + ',max(case 项目 when '''+cast(项目 as char(3))+''' then 值 end) ['+cast(项目 as char(3))+']'
     from (select distinct 项目 from 表) as a
    select @sql = @sql+' ,时间 from 表 group by 站名,时间'
    exec(@sql)站名          101        102        103        106        时间   
    ----------- ---------- ---------- ---------- ---------- ---- 
    1           0.01       0.02       0.03       0.05       5:00
    2           0.001      0.002      0.003      0.005      5:00
      

  2.   

    http://expert.csdn.net/Expert/topic/2440/2440306.xml?temp=.6941645
     [交流]行列转换
      

  3.   

    建议你使用存储过程完成
    简单的SQL也能做做,但是实用性不强
    select (case 项目 when '101' then SUM(值) end) as 101,(case 项目 when '102' then sum(值) end) as 102, ... ,'5:00' as 时间 from a group by 站名  
    因为项目名称可能是动态的
    所以最好在此之前先
    select distinct 项目 from a
    然后使用for循环自动组成如上的SQL语句
    例如:
    sqlsa = sqlsa + "case 项目 when " + rs.fields("项目").value + "then SUM(值) end) as" + rs.fields("项目").value 
    这是在VB中的使用
    可以,但是效率较低
    建立存储过程可以更高的效率,把select distinct 项目 from a封装成一个数据库函数,然后在你的存储过程调用。
    在存储过程中不要使用游标,效率很低而且极易引起内存溢出(大数据量统计)
      

  4.   

    不好意思,有点错误
    select SUM(case 项目 when '101' then 值 end) as 101,SUM(case 项目 when '102' then 值 end) as 102, ... ,'5:00' as 时间 from a group by 站名  
      

  5.   

    create table test(项目 varchar(5), 站名 int, 值 decimal(10,3),  时间 varchar(5))
    insert into test select '101',   1,   0.01,  '5:00'
    insert into test select '102',   1,   0.02,  '5:00'
    insert into test select '103',   1,   0.03,  '5:00'
    insert into test select '106',   1,   0.05,  '5:00'
    insert into test select '101',   2,   0.001,  '5:00'
    insert into test select '102',   2,   0.002,  '5:00'
    insert into test select '103',   2,   0.003,  '5:00'
    insert into test select '106',   2,   0.005,  '5:00'declare @sql varchar(8000)
    set @sql='select 站名'
     select @sql= @sql + ',sum(case 项目 when '''+ 项目 + ''' then 值 else 0 end) as [' + 项目 +']' 
     from (select distinct 项目 from test) as a
    select @sql=@sql+',时间 from test group by 站名,时间'
    select @sql
    exec(@sql)drop table test
      

  6.   

    create table test(项目 int, 站名 int, 值 decimal(10,3),  时间 varchar(5))
    insert into test select 101,   1,   0.01,  '5:00'
    insert into test select 102,   1,   0.02,  '5:00'
    insert into test select 103,   1,   0.03,  '5:00'
    insert into test select 106,   1,   0.05,  '5:00'
    insert into test select 101,   2,   0.001,  '5:00'
    insert into test select 102,   2,   0.002,  '5:00'
    insert into test select 103,   2,   0.003,  '5:00'
    insert into test select 106,   2,   0.005,  '5:00'declare @sql varchar(8000)
    set @sql='select 站名'
     select @sql= @sql + ',sum(case 项目 when '''+ cast(项目 as varchar(5))+ ''' then 值 else 0 end) as [' + cast(项目 as varchar(5)) +']' 
     from (select distinct 项目 from test) as a
    select @sql=@sql+',时间 from test group by 站名,时间'
    select @sql
    exec(@sql)drop table test
      

  7.   

    我忘了说了
    项目 站名 值  时间
    项目 站名 是固定的数目
    值跟 时间是变化的 一个小时一变化 
    我想 定义一个存储过程 跟一个JOB
    每小时 自动 把数据导入到新表
    参数是 时间..
    我按上面的方法试了
    可是 不好使啊...