一个表一个字段 f1
只有两条记录 比如 
f1 
18
12
怎么用sql语句求出 @xs=18/12 即两条记录的比值。
不用游标哦

解决方案 »

  1.   

    create table tb_04(f1 int)
    insert into tb_04 select 18 union all select 12--类似游标行不行
    declare @t table(id int identity(1,1),f1 int)
    insert into @t select f1 from tb_04
    select (select f1 from @t where id=1)*1.0/(select f1 from @t where id=2)
    /*
    1.500000000000
    */
      

  2.   

    create table tb_04(f1 int)
    insert into tb_04 select 18 union all select 12declare @s varchar(8000) set @s=''
    select @s=@s+cast(f1 as varchar(200))+',' from tb_04
    select @s=left(@s,len(@s)-1)
    select cast(left(@s,charindex(',',@s)-1) as int)*1.0/cast(substring(@s,charindex(',',@s)+1,len(@s)) as int)
    /*
    1.500000000000
    */
      

  3.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (f1 int)
    insert into #T
    select 18 union all
    select 12select id=identity(int,1,1),* into # from #Tselect a.f1*1.0/b.f1
    from # a
     join # b
       on a.id=b.id-1 and b.id%2=0
       
      

  4.   


    我没想出来 order by 对象该是啥 
      

  5.   

    SELECT (SELECT TOP 1 f1
              FROM t) /
              (SELECT TOP 1 f1
             FROM t
             ORDER BY f1 DESC) 
      

  6.   


    SELECT TOP 1 f1
              FROM t是18
    SELECT TOP 1 f1
            FROM t
            ORDER BY f1 DES
    也是18
      

  7.   

    在大家的帮助下,有了标准答案:
    declare @x numeric(9,2)
    select @x=max(f1)/min(f1) from (select f1 from tb) a
    哈哈
      

  8.   

    玩游戏,表都用了舍不得给字段
    declare @a int,@b int
    select @a=(select top 1 f1 from #t),@b=@a
    select @b=f1 from #t where f1<>@a
    select @b*1.0/@a
      

  9.   

    伟大的光荣的正确的ASP千岁千岁千千岁 --小心被拉下来打80大板
      

  10.   


    不怎么样
    如果是
    f1
    12
    18 
    呢?你的这个写法完全是已经知道数据了,才这么写的,与其这样,你还不如select 18*1.0/12呢
      

  11.   


    也是好办法。
    就是select @b*1.0/@a写反了,应该是select @a*1.0/@b
      

  12.   

    那你提前也没说啊 ~ ~ 
    那直接就 
    select max(f1)*1.0/min(f1) from tb
    不就行了
      

  13.   

    select max(f1)*1.0/min(f1) from tb嗯..这个 是楼主的问题.让大家都去猜了