SQL查詢連續(xù)號碼段的巧妙解法
昨天在itpub看到這個(gè)帖子, 問題覺得有意思,, 就仔細(xì)想了想. 也給出了一種解決辦法..:-)
問題求助,請高手指點(diǎn)..
我有一個(gè)表結(jié)構(gòu),
fphm,kshm
2014,00000001
2014,00000002
2014,00000003
2014,00000004
2014,00000005
2014,00000007
2014,00000008
2014,00000009
2013,00000120
2013,00000121
2013,00000122
2013,00000124
2013,00000125
(第二個(gè)字段內(nèi)可能是連續(xù)的數(shù)據(jù),可能存在斷點(diǎn)。)
怎樣能查詢出來這樣的結(jié)果,查詢出連續(xù)的記錄來。
就像下面的這樣?
2014,00000001,00000005
2014,00000009,00000007
2013,00000120,00000122
2013,00000124,00000125
方法一: 引用自hmxxyy.
SQL> select * from gap;
ID SEQ
---------- ----------
1 1
1 4
1 5
1 8
2 1
2 2
2 9
select res1.id, res2.seq str, res1.seq end
from (
select rownum rn, c.*
from (
select *
from gap a
where not exists (
select null from gap b where b.id = a.id and a.seq = b.seq - 1
)
order by id, seq
) c
) res1, (
select rownum rn, d.*
from (
select *
from gap a
where not exists (
select null from gap b where b.id = a.id and a.seq = b.seq + 1
)
order by id, seq
) d
) res2
where res1.id = res2.id
and res1.rn = res2.rn
/
ID STR END
--------- ---------- ----------
1 1 1
1 4 5
1 8 8
2 1 2
2 9 9
方法二: 使用lag/lead分析函數(shù)進(jìn)行處理.. 樓上的方法確實(shí)挺好用就是覺得表掃描/表連接比較多, 可能數(shù)據(jù)量大了. 速度會比較慢, 當(dāng)然我的這種方法由于使用分析函數(shù)使用的比較頻繁.所以排序量可能比上一種要多..
SQL> select fphm,lpad(kshm,8,'0') kshm
2 from t
3 /
FPHM KSHM
---------- ----------------
2014 00000001
2014 00000002
2014 00000003
2014 00000004
2014 00000005
2014 00000007
2014 00000008
2014 00000009
2013 00000120
2013 00000121
2013 00000122
FPHM KSHM
---------- ----------------
2013 00000124
2013 00000125
13 rows selected.
SQL> set echo on
SQL> @bbb.sql
SQL> select fphm,lpad(kshm,8,'0') start_kshm,lpad(prev_prev_kshm,8,'0') end_kshm
2 from (
3 select fphm,kshm,next_kshm,prev_kshm,
4 lag(kshm,1,null) over (partition by fphm order by kshm )next_next_kshm,
5 lead(kshm,1,null) over (partition by fphm order by kshm ) prev_prev_kshm
6 from (
7 select *
8 from (
9 select fphm,kshm,
10 lead(kshm,1,null) over (partition by fphm order by kshm) next_kshm,
11 lag(kshm,1,null) over (partition by fphm order by kshm) prev_kshm
12 from t
13 )
14 where ( next_kshm - kshm <> 1 or kshm - prev_kshm <> 1 )
15 or ( next_kshm is null or prev_kshm is null )
16 )
17 )
18 where next_kshm - kshm = 1
19 /
FPHM START_KSHM END_KSHM
---------- ---------------- ----------------
2013 00000120 00000122
2013 00000124 00000125
2014 00000001 00000005
2014 00000007 00000009
SQL> spool off
方法三: 今天早上wildflower給了我這個(gè)答案, 頓時(shí)覺得耳目一新啊..就貼出來與大家一起共享了^_^.
SQL> spool aaa.log
SQL> set echo on
SQL> select * from t;
no rows selected
SQL> select * from t;
FPHM KSHM
---------- ----------
2014 1
2014 2
2014 3
2014 4
2014 5
2014 7
2014 8
2014 9
2013 120
2013 121
2013 122
FPHM KSHM
---------- ----------
2013 124
2013 125
13 rows selected.
SQL> @bbb.sql
SQL> select b.fphm,min(b.kshm),max(b.kshm)
2 from (
3 select a.*,to_number(a.kshm-rownum) cc
4 from (
5 select * from t order by fphm,kshm
6 ) a
7 ) b
8 group by b.fphm,b.cc
9 /
FPHM MIN(B.KSHM) MAX(B.KSHM)
---------- ----------- -----------
2013 120 122
2013 124 125
2014 1 5
2014 7 9
SQL>
相關(guān)文章
Instagram提升PostgreSQL性能的五個(gè)技巧
這篇文章主要介紹了Instagram提升PostgreSQL性能的五個(gè)技巧,Instagram的數(shù)據(jù)庫一直由PostgreSQL支撐,經(jīng)驗(yàn)很具有參考性,需要的朋友可以參考下2015-04-04面向云服務(wù)的GaussDB全密態(tài)數(shù)據(jù)庫現(xiàn)狀及問題小結(jié)
全密態(tài)數(shù)據(jù)庫,顧名思義與大家所理解的流數(shù)據(jù)庫、圖數(shù)據(jù)庫一樣,就是專門處理密文數(shù)據(jù)的數(shù)據(jù)庫系統(tǒng),這篇文章主要介紹了面向云服務(wù)的GaussDB全密態(tài)數(shù)據(jù)庫,未來GaussDB會將該能力逐步開源到openGauss,與社區(qū)共同推進(jìn)和完善全密態(tài)數(shù)據(jù)庫解決方案,一起打造數(shù)據(jù)庫安全生態(tài)2024-02-02介紹PostgreSQL中的jsonb數(shù)據(jù)類型
這篇文章主要介紹了介紹PostgreSQL中的jsonb數(shù)據(jù)類型,jsonb是PostgreSQL9.4中開始內(nèi)置的類型,能夠支持GIN索引,需要的朋友可以參考下2015-04-04SQL注入報(bào)錯(cuò)注入函數(shù)圖文詳解
報(bào)錯(cuò)注入是SQL注入的一種,下面這篇文章主要給大家介紹了關(guān)于SQL注入報(bào)錯(cuò)注入函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07access mysql mssql 隨機(jī) 10條數(shù)據(jù)的sql語句
好多情況下,大家需要隨機(jī)抽取幾個(gè)數(shù)據(jù),當(dāng)然數(shù)據(jù)是從數(shù)據(jù)庫來的,所以腳本之家特為大家準(zhǔn)備了一些。2009-05-05Navicat 導(dǎo)出導(dǎo)入數(shù)據(jù)庫的實(shí)現(xiàn)方法
這篇文章主要介紹了Navicat 導(dǎo)出導(dǎo)入數(shù)據(jù)庫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11數(shù)據(jù)庫查詢優(yōu)化(主從表的設(shè)計(jì))
寫過許多的數(shù)據(jù)庫查詢語句,面對過許多的問題,也在磕磕碰碰中學(xué)到了很多東西。昨天在看系統(tǒng)數(shù)據(jù)庫設(shè)計(jì)與SQL代碼時(shí),突然“發(fā)現(xiàn)”了許多平常不屑看到的問題,于是就萌生了把它提出來思考的念頭2013-08-08