update a
set 
    Percent=rtrim(isnull(Value,0)*100/(select Value from #T where key=a.key and Order=1))+'%'
from 
    #T a

解决方案 »

  1.   

    --生成测试数据
    create table #T([Order] int,[Value] int,[Percent] varchar(10),[key] int)
    insert into #T select 1,100 ,null,1
    insert into #T select 2,50  ,null,1
    insert into #T select 3,30  ,null,1
    insert into #T select 1,170 ,null,3
    insert into #T select 2,170 ,null,3
    insert into #T select 3,85  ,null,3
    insert into #T select 4,50  ,null,3
    insert into #T select 5,null,null,4--执行更新操作
    update a
    set 
        [Percent]=rtrim(isnull(Value,0)*100/isnull((select Value from #T where [key]=a.[key] and [Order]=1),1))+'%'
    from  #T a--输出执行结果
    select * from #T
    /*
    1 100 100% 1
    2 50 50% 1
    3 30 30% 1
    1 170 100% 3
    2 170 100% 3
    3 85 50% 3
    4 50 29% 3
    5 NULL 0% 4
    */--删除测试数据
    drop table #T