使用SQL語句實現(xiàn)查詢連續(xù)號段
作者:佚名
在SQL語句中,有許多功能強大的語句,下面為您介紹的SQL語句用于實現(xiàn)查詢連續(xù)號段,希望對您有所幫助。
下面將教您如何使用SQL語句實現(xiàn)查詢連續(xù)號段,供您參考,如果您對SQL語句實現(xiàn)查詢連續(xù)號段這個方法感興趣的話,不妨一看,對您學習SQL語句使用會有所幫助。
- With tempTable As(
- select 2014 code,'00000001' tel from dual union all
- select 2014 code,'00000002' tel from dual union all
- select 2014 code,'00000003' tel from dual union all
- select 2014 code,'00000004' tel from dual union all
- select 2014 code,'00000005' tel from dual union all
- select 2014 code,'00000007' tel from dual union all
- select 2014 code,'00000008' tel from dual union all
- select 2014 code,'00000009' tel from dual union all
- select 2013 code,'00000120' tel from dual union all
- select 2013 code,'00000121' tel from dual union all
- select 2013 code,'00000122' tel from dual union all
- select 2013 code,'00000124' tel from dual union all
- select 2013 code,'00000125' tel from dual
- ),
- group_tempTable As(
- Select a.*, a.tel - Rownum 分組
- From (Select *
- From tempTable
- Order By code, tel) a
- )
- Select b.code, Min(b.tel) Start_Tel, Max(b.tel) End_Tel
- From group_tempTable b
- Group By b.code, b.分組
- Order By b.code, b.分組
執(zhí)行結(jié)果:
- CODE START_TEL END_TEL
- 1 2013 00000120 00000122
- 2 2013 00000124 00000125
- 3 2014 00000001 00000005
- 4 2014 00000007 00000009
【編輯推薦】
責任編輯:段燃
來源:
互聯(lián)網(wǎng)