好象用不多于两条select语句很难实现,即使实现了对数据库的负载也是比较重的,
其实可以变相思维,在程序中实现不是同样可以实现吗?

解决方案 »

  1.   

    --test:e.g.create table #t(id int,money int,name varchar(10))
    insert #t
    select 1,500,'a'
    union select 2,450,'b'
    union select 3,400,'c'
    union select 4,300,'d'
    select * from #tselect * from #t a where 
    (select sum(money) from #t where id<=a.id)
    <=
    (select sum(money)*0.8 from #t)drop table #t
      

  2.   

    select * from 表a a where 
    (select sum(money) from 表a where id<=a.id)<=(select sum(money)*0.8 from #t)
      

  3.   

    select * from 表a a where 
    (select sum(money) from 表a where id<=a.id)<=(select sum(money)*0.8 from 表a)
      

  4.   


    select id,money, name
    from table a ,
    (select sum(money)*0.8 as totalmoney from table ) b
    where isnull( money+(select sum(money) from table where id<a.id),money)<=b.totalmoney