问题是这样的,我创建了一个表CostTable,专门用来放需要缴费的项目,表格式如下:
           prj      amount     year
           X1费     10000       1
           X2费     5000        1
           X3费     8000        1
           X4费     35000       2
           X5费     10000       2
           X6费     10000       3
现在需要实现一张临时表,当‘year == 1’时,表的结构为
X1费     X2费    X3费
10000   5000     8000
当‘year == 2’时,表的结构为
X4费     X5费
35000   10000    
刚学写程序,只会写点SQL语句,对这种问题束手无策,谢谢大家了哦~~~

解决方案 »

  1.   

    create table aggg(prj varchar(10), amount decimal(10,0), year int)
    insert aggg select
    'X1费', 10000, 1
    union all select 'X2费', 5000 ,1
    union all select 'X3费', 8000 ,1
    union all select 'X4费', 35000, 2
    union all select 'X5费', 10000, 2
    union all select 'X6费', 10000 ,3create proc pt
    @y int
    as 
    if object_id('表') is not null 
    drop table 表
    declare @s varchar(8000)
    set @s=''
    select @s=@s+'sum(case when [prj]='''+[prj]+''' then amount else 0 end ) '+[prj]+',' from aggg where [year]=@y
    select @s='select '+left(@s,len(@s)-1) +' into 表 from aggg where [year]=' +ltrim(@y)
    exec(@s)
    select * from 表
    gopt 1
      

  2.   

    declare @sql varchar(8000)
    declare @year int ---此参数为你要查询的年
    set @sql=''
    select @sql=@sql+' select (case when  prj='''+ prj+''' when  amount end) ['''+ prj+'''] into from CostTable from CostTable' from CostTable where [year]=@year
    select @sql=@sql+' select * from #Tmp '
    exec(@sql)
      

  3.   

    虽然看不懂,还是谢谢大家了。
    to  chuifengde() :
    CostTable 表里的数据不是固定的,随时会增会减的。
      

  4.   

    create table CostTable(prj varchar(10), amount decimal(10,0), year int)
    insert CostTable select
    'X1费', 10000, 1
    union all select 'X2费', 5000 ,1
    union all select 'X3费', 8000 ,1
    union all select 'X4费', 35000, 2
    union all select 'X5费', 10000, 2
    union all select 'X6费', 10000 ,3create proc pro_test(@year int)  --此参数为你要查询的年
    as
    begin
    declare @sql varchar(8000)
    set @sql='select'
    select @sql=@sql+'  sum(case when  prj='''+ prj+''' then  amount end) ['+ prj+'],' from CostTable where [year]=@year
    select @sql=left(@sql,len(@sql)-1)
    select @sql=@sql+' into #Tmp from CostTable select * from #Tmp '
    --print @sql
    exec(@sql)
    end
      

  5.   

    根据条件随时创建 create table #表名  随时删除就可以了  drop table #表名  
    表名加个变量  declare @tablename varchar(100)
    set @tablename='#mingzi'
     create table  @tablename 
    字段你自己定。
      

  6.   

    create table aggg(prj varchar(10), amount decimal(10,0), year int)
    insert aggg select
    'X1费', 10000, 1
    union all select 'X2费', 5000 ,1
    union all select 'X3费', 8000 ,1
    union all select 'X4费', 35000, 2
    union all select 'X5费', 10000, 2
    union all select 'X6费', 10000 ,3
    select a.year,
    'x1'=sum(isnull(case when a.year=1 and a.prj='X1费'then a.amount end,0))
    ,'x2'=sum(isnull(case when a.year=1 and a.prj='X2费'then a.amount end,0))
    ,'x3'=sum(isnull(case when a.year=1 and a.prj='X3费'then a.amount end,0))
    ,'x4'=sum(isnull(case when a.year=2 and a.prj='X4费'then a.amount end,0))
    ,'x5'=sum(isnull(case when a.year=2 and a.prj='X5费'then a.amount end,0))
    ,'x6'=sum(isnull(case when a.year=3 and a.prj='X6费'then a.amount end,0))
     from aggg a 
    group by year
      

  7.   

    create table #t(prj varchar(10), amount decimal(10,0), year int)
    insert #t select
    'X1费', 10000, 1
    union all select 'X2费', 5000 ,1
    union all select 'X3费', 8000 ,1
    union all select 'X4费', 35000, 2
    union all select 'X5费', 10000, 2
    union all select 'X6费', 10000 ,3
    declare @r varchar(4000)
    set @r=''
    select @r=@r+'['+prj+'] =sum(case prj when '''+prj+''' then amount  else 0 end),'
    from #t  where year=1 group by prj --year是这里的条件
    select @r=left(@r,len(@r)-1)
    set @r='select '+@r+' from #t '
    exec(@r)
    X1费                                      X2费                                      X3费                                      
    ---------------------------------------- ---------------------------------------- ---------------------------------------- 
    10000                                    5000                                     8000
      

  8.   

    刚开始写就看一下,动态查询的原理如下:
    select 
    [X1费]=sum(case prj when 'X1费' then amount else 0 end),
    [X2费]=sum(case prj when 'X2费' then amount else 0 end),
    [X3费]=sum(case prj when 'X3费' then amount else 0 end),
    [X4费]=sum(case prj when 'X4费' then amount else 0 end),
    [X5费]=sum(case prj when 'X5费' then amount else 0 end),
    [X6费]=sum(case prj when 'X6费' then amount else 0 end)
    from #t
      

  9.   

    在select * into # 加#生成临时表#