table(name,date,int)
合计int时候,求sum(int)>=100的最小时间,sum(int)>=120的最小时间
就是sum(int)>=100时候的时间是多少

解决方案 »

  1.   

    select * from tb a
    where  (select sum(int) from tb where date<a.date)<100
       abd (select sum(int) from tb where date<a.date)+a.int>100
      

  2.   

    select * from tb a
    where  (select sum(int) from tb where date<a.date)<100
       and (select sum(int) from tb where date<a.date)+a.int>100
    -- 如果按name分组求
    select * from tb a
    where  (select sum(int) from tb where name=a.name and date<a.date)<100
       and (select sum(int) from tb where name=a.name and date<a.date)+a.int>100
      

  3.   

    --求sum(int)>=100的最小时间
    select min(date)
    from (
    select name,date,int,flag=case when (select sum(int) from table where name<=a.name)>=100 then 1
    else 0 end
    from table a
         ) new
    where flag=1
    --sum(int)>=120的最小时间
    select min(date)
    from (
    select name,date,int,flag=case when (select sum(int) from table where name<=a.name)>=120 then 1
    else 0 end
    from table a
         ) new
    where flag=1--sum(int)>=100时候的时间是多少select date
    from (
    select name,date,int,flag=case when (select sum(int) from table where name<=a.name)<100 then 0
    else case when (select sum(int) from table where name<a.name)>=100 then 2
    else  1 end
    end
    from table a
         ) new
    where flag=1
      

  4.   

    ------------
    select * from [table] group by [name],[date],[int] having sum([int])>=100
      

  5.   

    邹老师,如果第一个数的int=100的话,你取得的结果好象就有问题,这个怎么改
    还有就是sum(int)+int=100的这种情况好象也有问题,改为>=100好象就对了
      

  6.   

    select * from tb a
    where  (select sum(int) from tb where date<a.date)<100
       and (select sum(int) from tb where date<a.date)+a.int>=100  -- 包含100的这种情况
      

  7.   

    邹老师,如果第一个数的int=100的话 这个条件就找不到了 因为最小sum(int)=100
    这个怎么办
      

  8.   

    不会存在你说的问题啊.(select sum(int) from tb where date<a.date)  这个不统计当前记录. 所以第1条记录为100时, 这个表达式值为0, 满足<100的条件
    而第2个表达式的计算结果为100, 满足>=100的条件
      

  9.   

    id       bh       date          int240 BB 2005-10-1 100 100
    241 BB 2005-10-2 18  10
    242 BB 2005-10-3 3   3
    243 BB 2005-10-4 2   5我怎么不能求出BB的时间呀
      

  10.   

    DECLARE @t TABLE(id int,bh char(2), date datetime, [int] int)
    INSERT @t SELECT '240','BB','2005-10-1','100'
    UNION ALL SELECT '241','BB','2005-10-2','18'
    UNION ALL SELECT '242','BB','2005-10-3','3'
    UNION ALL SELECT '243','BB','2005-10-4','2'select *
    from @t a
    where  isnull((select sum(int) from @t where date<a.date),0)<100
       and isnull((select sum(int) from @t where date<a.date),0)+a.int>=100  
    -- 结果
    id          bh   date                                                   int         
    ----------- ---- ------------------------------------------------------ ----------- 
    240         BB   2005-10-01 00:00:00.000                                100(所影响的行数为 1 行)