需要检索不在所选列表的开头或末尾的n行

时间:2011-05-16 15:06:43

标签: oracle10g

我写过sql语句:

    select * from (
    select count(*) as NumberofSignals,signals.transmitter_account,signals.class,signals.type,signals.signal_mode,
    signals.area_id,signals.sector_id,signals.region_info_id,signals.zone_info_id,signals.user_id,signals.device_id,
    signals.panel_name,signals.panel_id,signals.sector_name,signals.region_code,signals.area_name,signals.zone_code,
    signals.description,signals.transmitter_name,signals.transmitter_id,signals.color,'event' as Event,get_name(signals.id,'event') as event_value,
    'packetnumber' as packetnumber,get_name(signals.id,'packetnumber') as packetnumber_value,wm_concat(distinct get_name(signals.id,'repeater')) as repeater,
    round(avg(get_name(signals.id,'signallevel'))) as avg_signallevel,min(to_char(signals.signal_forming_time, 'yyyy/mm/dd hh24:mi:ss')) as formingtime,
    get_name(signals.id,'address') as address,get_name(signals.id,'username') as username,get_name(signals.id,'chaneltype') as channeltype,
    get_name(signals.id,'code') as code,get_name(signals.id,'account') as account
    from signals,signal_custom_fields where signals.id = signal_custom_fields.signal_id and
    signals.id in (select id from (select id,rownum num from((select signals.id
    from signals,signal_custom_fields  where signal_custom_fields.field_name = 'event' 
    and signal_custom_fields.field_value is not null  and signals.id = signal_custom_fields.signal_id 
    and signals.signal_forming_time >= to_date('2011/5/10 14:34:44', 'yyyy/mm/dd hh24:mi:ss') 
    AND signals.signal_forming_time <= to_date('2011/5/10 15:34:44', 'yyyy/mm/dd hh24:mi:ss'))
    intersect (select distinct signals.id from signals,signal_custom_fields  
    where signal_custom_fields.field_name = 'packetnumber' and signal_custom_fields.field_value is not null 
    and signals.id = signal_custom_fields.signal_id 
    and signals.signal_forming_time >= to_date('2011/5/10 14:34:44', 'yyyy/mm/dd hh24:mi:ss') 
    AND signals.signal_forming_time <= to_date('2011/5/10 15:34:44', 'yyyy/mm/dd hh24:mi:ss'))) 
    order by id desc)) group by 'event',signals.transmitter_account,signals.class,
    signals.type,signals.signal_mode,signals.area_id,signals.sector_id,signals.region_info_id,signals.zone_info_id,
    signals.user_id,signals.device_id,signals.panel_name,signals.panel_id,signals.sector_name,signals.region_code,
    signals.area_name,signals.zone_code,signals.description,signals.transmitter_name,signals.transmitter_id,
    signals.color, get_name(signals.id,'event'), 'packetnumber',get_name(signals.id,'username'),
   get_name(signals.id,'chaneltype'),
   get_name(signals.id,'code'),
   get_name(signals.id,'account'), get_name(signals.id,'packetnumber'),get_name(signals.id,'address'),
   TO_CHAR(signals.signal_forming_time ,'dd/mm/yyyy hh24'),
   TRUNC(to_number(to_char(signals.signal_forming_time ,'mi'))/(30))
   order by event)where rownum < 300  

在这里我获得前300行,但我需要如何重写此语句以检索第二行300

1 个答案:

答案 0 :(得分:0)

您的查询没有第一个嵌套表中列出的rownum。在第一个嵌套表中添加一个rownum列,然后你可以在顶层的where子句中执行一个between函数:

--create a demo table
DROP TABLE paging_test;
CREATE TABLE paging_test AS
  (SELECT rownum x FROM user_tables
  );

--count how many records exist (in my case there is 821)
SELECT COUNT(*)
FROM paging_test;

--get the first 300 rows
SELECT *
FROM
  (SELECT rownum rn, x FROM paging_test ORDER BY x
  ) pt
WHERE pt.rn BETWEEN 1 AND 300 ;

--get the next 300 rows
SELECT *
FROM
  (SELECT rownum rn, x FROM paging_test ORDER BY x
  ) pt
WHERE pt.rn BETWEEN 300 AND 600 ;

您可能也对我的参考感兴趣:

参考文献: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:948366252775