1. 程式人生 > >關於Oracle中排序後取前幾行的問題

關於Oracle中排序後取前幾行的問題

               

 查詢語句一、

    SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
       FROM T_VEHICLE_PHOTO V
      WHERE V.ISMANAGE = 0
        AND V.TOUCH_OFF = 27
        AND V.NUMBER_PLATE = '蘇F14899'
        AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
        AND ROWNUM <= 6


      ORDER BY V.STAMP desc

查詢語句二、

     SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
       FROM T_VEHICLE_PHOTO V
      WHERE V.ISMANAGE = 0
        AND V.TOUCH_OFF = 27
        AND V.NUMBER_PLATE = '蘇F14899'
        AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
        AND ROWNUM <6


      ORDER BY V.STAMP desc

查詢語句一的結果:

STAMP

2008-11-2 8:11:52
2008-11-1 8:02:09
2008-10-31 17:16:02
2008-10-31 13:41:58
2008-10-31 10:53:00
2008-10-31 7:24:57

查詢語句二的結果:

STAMP

2008-11-1 8:02:09
2008-10-31 17:16:02
2008-10-31 13:41:58
2008-10-31 10:53:00
2008-10-31 7:24:57

不加行數限制的結果:

2008-11-2 8:11:52
2008-11-2 7:27:57
2008-11-1 17:44:29
2008-11-1 14:32:07
2008-11-1 11:44:59
2008-11-1 8:02:09
2008-10-31 17:16:02
2008-10-31 13:41:58
2008-10-31 10:53:00
2008-10-31 7:24:57
2008-10-30 17:36:48

由此可以看出,如果不使用<=你拿到的資料會少一行,並且很奇怪是第一行,正常要想法應是最後一行才對,那麼現在在比較以下兩種寫法:

select 標題,釋出時間 from (select 標題,釋出時間 from hhsys.tongzhigonggao order by 釋出時間 desc) where rownum<11 

這樣是拿排序後的前10

select 標題,釋出時間 from hhsys.tongzhigonggao where rownum<=10order by 釋出時間 desc

這樣的話應是拿到前10行再進行排序,但他實際上又是拿到排序後的前10行,真搞不明白。

噢,弄明白,了上面的語句寫法是錯誤的。

正確的語句如下:

     SELECT * FROM (
     SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
       FROM T_VEHICLE_PHOTO V
      WHERE V.ISMANAGE = 0
        AND V.TOUCH_OFF = 27
        AND V.NUMBER_PLATE = '蘇F14899'
        AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS') 
      ORDER BY V.STAMP DESC
      ) WHERE ROWNUM <= 6

也就是,如果你要取排序後的結果,不能將ROWNUM <= 6放在order by前面。

=======================================================================

     SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
       FROM T_VEHICLE_PHOTO V
      WHERE V.ISMANAGE = 0
        AND V.TOUCH_OFF = 27
        AND V.NUMBER_PLATE = '蘇F14899'
        AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')

正常資料:

1 2008-10-31 7:24:57
2 2008-10-31 10:53:00
3 2008-10-31 13:41:58
4 2008-10-31 17:16:02
5 2008-11-1 8:02:09
6 2008-11-2 8:11:52
7 2008-11-1 11:44:59
8 2008-11-1 14:32:07
9 2008-11-1 17:44:29
10 2008-11-2 7:27:57
11 2008-10-30 17:36:48

===================================================

     SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
       FROM T_VEHICLE_PHOTO V
      WHERE V.ISMANAGE = 0
        AND V.TOUCH_OFF = 27
        AND V.NUMBER_PLATE = '蘇F14899'
        AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
        AND ROWNUM <= 6

取前6行資料:
1 2008-10-31 7:24:57
2 2008-10-31 10:53:00
3 2008-10-31 13:41:58
4 2008-10-31 17:16:02
5 2008-11-1 8:02:09
6 2008-11-2 8:11:52

=========================================================

     SELECT V.STAMP,TO_CHAR(V.GPSTIME,'YYYY-MM-DD HH24:MI:SS'),V.PHOTOURL,V.SMALLPHOTOURL,V.TOUCH_OFF,V.VIDICON_NO,V.SPEED,V.COURSE,V.STATE
       FROM T_VEHICLE_PHOTO V
      WHERE V.ISMANAGE = 0
        AND V.TOUCH_OFF = 27
        AND V.NUMBER_PLATE = '蘇F14899'
        AND V.STAMP < TO_DATE('2008-11-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS')
        AND ROWNUM <= 6
      ORDER BY V.STAMP DESC
取前6行然後排序的資料:

1 2008-11-2 8:11:52
2 2008-11-1 8:02:09
3 2008-10-31 17:16:02
4 2008-10-31 13:41:58
5 2008-10-31 10:53:00
6 2008-10-31 7:24:57

總結:

取排序後的前幾行,應該用:

select * from(select * from test order by stamp desc) where rownum<= 6  (表示排序後取前幾行)

而不應該使用:

select * from test where rownum<= 6  order by stamp desc (表示取前幾行後再排序)