SQL技巧之查询连续的记录

http://oracle.chinaitlab.com/PLSQL/913184.html

create table test_num

  (tyear number,

  tdate DATE);


insert into test_num

  select 2014,trunc(sysdate)-1 from dual union all

  select 2014,trunc(sysdate)-002 from dual union all

  select 2014,trunc(sysdate)-003 from dual union all

  select 2014,trunc(sysdate)-004 from dual union all

  select 2014,trunc(sysdate)-005 from dual union all

  select 2014,trunc(sysdate)-007 from dual union all

  select 2014,trunc(sysdate)-008 from dual union all

  select 2014,trunc(sysdate)-009 from dual union all

  select 2013,trunc(sysdate)-120 from dual union all

  select 2013,trunc(sysdate)-121 from dual union all

  select 2013,trunc(sysdate)-122 from dual union all

  select 2013,trunc(sysdate)-124 from dual union all

  select 2013,trunc(sysdate)-125 from dual union all

  select 2013,trunc(sysdate)-127 from dual union all

  select 2015,trunc(sysdate)-099 from dual union all

  select 2015,trunc(sysdate)-100 from dual union all

  select 2015,trunc(sysdate)-101 from dual union all

  select 2015,trunc(sysdate)-102 from dual union all

  select 2015,trunc(sysdate)-104 from dual union all

  select 2015,trunc(sysdate)-105 from dual;

SELECT * FROM test_num  ORDER BY TYEAR, TDATE;

  SELECT TYEAR, MIN(TDATE) AS STARTDATE, MAX(TDATE), COUNT(TYEAR) AS ENDNUM

  FROM (SELECT A.*, A.TDATE - ROWNUM AS GNUM

  FROM (SELECT * FROM TEST_NUM ORDER BY TYEAR, TDATE) A)

  GROUP BY TYEAR, GNUM

  ORDER BY TYEAR, MIN(TDATE);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值