成都网站建设设计

将想法与焦点和您一起共享

使用SQL语句实现查询连续号段

下面将教您如何使用SQL语句实现查询连续号段,供您参考,如果您对SQL语句实现查询连续号段这个方法感兴趣的话,不妨一看,对您学习SQL语句使用会有所帮助。

 
 
 
  1. With tempTable As(  
  2.   select 2014 code,'00000001' tel from dual union all  
  3.   select 2014 code,'00000002' tel from dual union all  
  4.   select 2014 code,'00000003' tel from dual union all  
  5.   select 2014 code,'00000004' tel from dual union all  
  6.   select 2014 code,'00000005' tel from dual union all  
  7.   select 2014 code,'00000007' tel from dual union all  
  8.   select 2014 code,'00000008' tel from dual union all  
  9.   select 2014 code,'00000009' tel from dual union all  
  10.   select 2013 code,'00000120' tel from dual union all  
  11.   select 2013 code,'00000121' tel from dual union all  
  12.   select 2013 code,'00000122' tel from dual union all  
  13.   select 2013 code,'00000124' tel from dual union all  
  14.   select 2013 code,'00000125' tel from dual  
  15. ),  
  16. group_tempTable As(  
  17.   Select a.*, a.tel - Rownum 分组  
  18.     From (Select *  
  19.             From tempTable  
  20.            Order By code, tel) a  
  21. )  
  22. Select b.code, Min(b.tel) Start_Tel, Max(b.tel) End_Tel  
  23.   From group_tempTable b  
  24.  Group By b.code, b.分组  
  25.  Order By b.code, b.分组 

执行结果:

 
 
 
  1.    CODE    START_TEL    END_TEL  
  2. 1    2013    00000120    00000122  
  3. 2    2013    00000124    00000125  
  4. 3    2014    00000001    00000005  
  5. 4    2014    00000007    00000009 

 

 

 

【编辑推荐】

单表多条件查询sql语句写法

使用SQL语句查询时间段

巧用SQL语句删除重复记录

两种数据库中查询表主键外键信息的SQL语句

教您使用SQL语句修改SQL主键


当前文章:使用SQL语句实现查询连续号段
网站URL:https://chengdu.cdxwcx.cn/article/copdssd.html