you might need to use cursor and temporary table

解决方案 »

  1.   

    Thanks .But I'm not willing to .
      

  2.   

    SELECT SUM(field3) AS Expr1, field1,field2
    FROM table1
    GROUP BY field1,field2
      

  3.   

    在sql server 2000中测试成功,如下:
    select t9.field1,t9.field2,sum(field3)
    from (
    select t1.field1,t1.field2,t2.field3
    from test t1,test t2
    where  t1.field1=t2.field1
    and (
    t1.field2=t2.field2  or 
    ( datediff(dd,t1.field2,t2.field2)>0
    and datediff(dd,t1.field2,t2.field2)=
    (select count(*)-1  from test t3 where 
    t3.field1=t1.field1 and 
    t3.field2 between t1.field2 and t2.field2)))
    and t1.field2 not in 
    (select t6.field2 from test t5 ,test t6 where t5.field1=t1.field1 and
    t5.field1=t6.field1 and t6.field2=dateadd(day,1,t5.field2))

    ) t9
    group by t9.field1,t9.field2
    order by t9.field1,t9.field2
      

  4.   

    1. create table result as select * from tab where 1=2;
    2. 建立一个存储过程:先对TAB按时间从大到小排序,设立一个变量用于保存相邻记录的和,每遇到一个不连续点就insert一次。一个循环搞掂。返回RESULT表就是了。看来只能这样了,你想用一条SQL语句看来是不现实的。
      

  5.   

    select field1,field2,sum(field3) from table group by field1,field2
      

  6.   

    唉。TAB符号贴上去怎么都没了??再贴一次吧,要不我自己看着都晕:-)select t9.field1,t9.field2,sum(field3)
    from (
       select t1.field1,t1.field2,t2.field3
       from test t1,test t2
       where  t1.field1=t2.field1
          and (t1.field2=t2.field2  or 
                (datediff(dd,t1.field2,t2.field2)>0
                and datediff(dd,t1.field2,t2.field2)=
                (select count(*)-1  from test t3 where 
                t3.field1=t1.field1 and 
                t3.field2 between t1.field2 and t2.field2)))
          and t1.field2 not in 
               (select t6.field2 from test t5 ,test t6 where t5.field1=t1.field1 and
                 t5.field1=t6.field1 and t6.field2=dateadd(day,1,t5.field2))
    ) t9
    group by t9.field1,t9.field2
    order by t9.field1,t9.field2