使用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语句实现查询连续号段
浏览路径:http://www.hantingmc.com/qtweb/news23/459823.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联