create table #tb(指标类型 nvarchar(100),指标属性 nvarchar(100)) 
insert into #tb
select 'wew','%%'
union all select 'FA','AFDA%%'
union all select '3323','sss'
godeclare @sql varchar(8000)
set @sql=''
select @sql=@sql + ',['+rtrim(指标类型)+']=max(case rtrim(指标类型) when '''+rtrim(指标类型)+''' then 指标属性 end)'
from #tb group by rtrim(指标类型)
set @sql=stuff(@sql,1,1,'')
exec('select '+@sql+' from #tb ')
drop table #tb/*
3323 FA wew
sss AFDA%% %%
*/

解决方案 »

  1.   


    谢谢,你这个方法是能得到这个,但是我需要的是在水晶报表里面得到这样的格式
    在水晶报表里面3323,FA,wew,这三个是以字段的形式出来的
    在水晶报表里面出来的就变成了只有sss AFDA%% %%  这些值了
    我需要的是
    3323    FA    wew
    sss    AFDA%%    %%
    这些都出来
    麻烦您再帮我看一下吧
    这是我的代码
      

  2.   

    [3323]=max(case rtrim(IndexType) when '3323' then IndexAttribute end),
    [FA]=max(case rtrim(IndexType) when 'FA' then IndexAttribute end),
    [wew]=max(case rtrim(IndexType) when 'wew' then IndexAttribute end),    
    这个是由
    -------------------------------------------------
    DECLARE @SqlString VARCHAR(8000)
    set @SqlString=''
    select @SqlString=@SqlString + ',['+rtrim(IndexType)+']=max(case rtrim(IndexType) when '''+rtrim(IndexType)+''' then IndexAttribute end)'
    from dbo.QIANYUEHETONG_Detail2 group by rtrim(IndexType)
    set @SqlString=stuff(@SqlString,1,1,'')
    --------------------------------------------------
    这个得出来的