最近在做项目的时候,被一个查询难倒了,不知道那位牛人能够帮助下,具体需求是这样的:
在一张百万甚至千万数据的表中 要查询两个日期(日期是精确到秒的)间(比如2011-04-23,2011-05-16)温度的变化,举个例子 在2011-04-23 00:00:00时 温度值为5,随着时间的升序 直到下个温度值不为5的符合条件(比如下个温度为6),在下个记录温度不为6符合条件,也就是说随着时间的升序下条记录的温度和上个记录的温度不一样的数据符合条件
写了个Sql供牛人参考,当然效率很低   select tc.rq, tc.upt1, tc.mdlt1, tc.downt1
     from (select rq,
                  ta.fj039 upt1,
                  ta.fj038 mdlt1,
                  ta.fj037 downt1,
                  lead(ta.fj039, 1) over(order by ta.rq) upt2,
                  lead(ta.fj038, 1) over(order by ta.rq) mdlt2,
                  lead(ta.fj037, 1) over(order by ta.rq) downt2
             from nz_fj ta
            where ta.rq >= date '2011-05-05'
              and ta.rq < date '2011-05-06') tc
    where tc.upt1 - tc.upt2 <> 0
       or tc.mdlt1 - tc.mdlt2 <> 0
       or tc.downt1 - tc.downt2 <> 0
不知道那位牛人写出个查询快点的Sql来

解决方案 »

  1.   

    这种思路已经比较精简了,
    不清楚你的数据,是否可改成
    SELECT tc.rq, tc.upt1, tc.mdlt1, tc.downt1
      FROM (SELECT rq,
                   ta.fj039 upt1,
                   ta.fj038 mdlt1,
                   ta.fj037 downt1,
                   lead(ta.fj039 || ta.fj038 || ta.fj037, 1) over(ORDER BY ta.rq) upt2,
              FROM nz_fj ta
             WHERE ta.rq >= DATE '2011-05-05'
               AND ta.rq < DATE '2011-05-06') tc
     WHERE tc.upt1 || tc.mdlt1 || tc.downt1 <> tc.upt2;
      

  2.   

    SELECT tc.rq, tc.upt1, tc.mdlt1, tc.downt1
      FROM (SELECT rq,
                   ta.fj039 upt1,
                   ta.fj038 mdlt1,
                   ta.fj037 downt1,
                   lead(ta.fj039 || ta.fj038 || ta.fj037, 1) over(ORDER BY ta.rq) upt2,
              FROM nz_fj ta
             WHERE ta.rq >= DATE '2011-05-05'
               AND ta.rq < DATE '2011-05-06') tc
     WHERE tc.upt1 || tc.mdlt1 || tc.downt1 <> tc.upt2;