select a=aa.a1/bb.a1,b=aa.b1/bb.b1,c=aa.c1/bb.c1
from
(select 1 as row,* from table where id = 1) aa,
(select 1 as row,* from table where id = 2) bb
where aa.row = bb.row

解决方案 »

  1.   

    declare @table1 table(id int,a1 int,b1 int,c1 int)
    insert into @table1 select 1,65,59,92
    insert into @table1 select 2,38,42,63declare @table2 table(a varchar(10),b varchar(10),c varchar(10))
    insert into @table2 
    select  
        rtrim(cast(b.a1*100.0/a.a1 as numeric(8,2)))+'%',
        rtrim(cast(b.b1*100.0/a.b1 as numeric(8,2)))+'%',
        rtrim(cast(b.c1*100.0/a.c1 as numeric(8,2)))+'%'
    from 
        @table1 a,@table1 b
    where
        a.id=1 and b.id=2select * from @table2/*
    a          b          c          
    ---------- ---------- ---------- 
    58.46%     71.19%     68.48%
    */
      

  2.   

    table1表只有这两条记录??
    =========
    當然有很多記錄,不過是不同年份的記錄,還有一個字段是存放年份的我沒寫出來,加上年份條件出來的就只有兩條記錄,a1,b1,c1...就是1月份,2月份,3月份....12月份.等我測試上面朋友給的語句先
      

  3.   

    declare @table1 table(id int,a1 int,b1 int,c1 int)
    insert into @table1 select 1,65,59,92
    insert into @table1 select 2,38,42,63declare @table2 table(a money,b money,c money)
    insert into @table2 
    select  
        round(convert(money,b.a1)/a.a1*100,2),
        round(convert(money,b.b1)/a.b1*100,2),
        round(convert(money,b.c1)/a.c1*100,2)
    from 
        (select * from @table1 where id = 1) a,
        (select * from @table1 where id = 2) b
    select * from @table2
      

  4.   

    insert into table1
    select  
        cast(b.a1*100.0/a.a1 as numeric(8,2))+'%',
        cast(b.b1*100.0/a.b1 as numeric(8,2))+'%',
        cast(b.c1*100.0/a.c1 as numeric(8,2))+'%'
    from 
        table1 a,table1 b
    where
        a.id=1 and b.id=2
      

  5.   

    謝謝大家,是asp+sql server2000的,rtrim(cast(b.a1*100.0/a.a1 as numeric(8,2)))+'%'
    老是提示運算式出錯,
    用convert會出錯:運算式中未定義的 'convert' 函數。改成下面就成功了,
    rtrim(Round((b.month1/a.month1*100),2))+'%'謝謝,結帖