假设有表id   theDate     score
1   2011-12-01    985
2   2011-12-02    456
3   2011-12-03    258我想求每一天比昨天 score 递增的百分比,我使用子查询的方法,但我发现在用于数据量10万行级别的时候,速度很慢,要10多秒,请问用什么方法可以比较快的运算出来,请给个例子.

解决方案 »

  1.   

    我原先的方法:
     select (score-(select score from table where theDate..... ))/(select score from table where theDate..... ) .....
      

  2.   

    the data is continous or not?
      

  3.   

    1.日期不一定连续的
    2.而且有时候可能是和前X天的score比较增幅的百分比
      

  4.   

    之前有个人问相邻数据的差。
    http://topic.csdn.net/u/20111225/23/524a2779-6598-42d3-b992-4459fc166cb9.html
    我写了一个sql语句。但是感觉不是很犀利。
    不过作为交流你可以用这个来测试一下效率。另外还有一种想法。是不是可以做一个多一个字段“上次的数据”的镜像表啊。这样查询起来更直接些
      

  5.   

    你可以用错位连接的方式试下。
    select ... from tb a left join tb b on (datediff(a.date,b.date)=1) 
    大致这个意思,具体可以用 row_number() 生成序列号,或者时间自己处理下
      

  6.   

    用左连接测试速度挺快,16万条数据,不到1秒钟
    select a.thedate,(a.score-b.score)*1.0/b.score 
    from tb a
    left join tbs b
    on a.id=b.id+1
      

  7.   

    id theDate score
    select top 1 id,theDate,score,null,null from table order by id
    union
    (
    select a.id,a.theDate,a.score,b.theDate,b.score,(a.score/b.score -1) p
    from table a,table b where a.id-1=b.id
    )