我用的是INTERBASE 
我的表的结果如下:
deptid   value   year
01       2000    2000
02       1500    2001
03       695     2003 
01        2051   2001
02        789    2000
...................
...................
我现在想的是要把表的结果道成:
id    2000   2001    2002  .....
01    2000   2051    .....
02    789    1500    .....
............................
我知道好象是要用到存储过程,但我不会。那为大哥能不 能给说的详细一些

解决方案 »

  1.   

    declare @sql varchar(1000)
    set @sql ='select id'
    select @sql=@sql+',sum(case year when '''+year+''' then value and)['+year+']'
    from (select destinct year from 表 ) as a
    setect @sql=@sql='from testgroup by id'
    exec(@sql)
      

  2.   

    我用的是INTERBASE 好想不行呀
      

  3.   

    试试这个语句:
     select id,sum(a) a,sum(b) b,sum(c) c,sum(d) d from 
      (
      select id,value_num as a,0 as b,0 as c, 0 as d from table where year='2000'
      union 
      select id,0 as a,value_num as b,0 as c, 0 as d from table where year='2001'
      union
      select id,0 as a,0 as b,value_num as c, 0 as d from table where year='2002'
      union
      select id,0 as a,0 as b,0 as c,value_num as d from table where year='2003' )
      group by id