oracle tips
===========================================================
如何sql查询出连续号码段
===========================================================
昨天在itpub看到这个帖子, 问题觉得有意思,, 就仔细想了想. 也给出了一种解决办法..:-)
问题求助,请高手指点..

我有一个表结构,
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

(第二个字段内可能是连续的数据,可能存在断点。)

怎样能查询出来这样的结果,查询出连续的记录来。
就像下面的这样?
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分析函数进行处理.. 楼上的方法确实挺好用就是觉得表扫描/表连接比较多, 可能数据量大了. 速度会比较慢, 当然我的这种方法由于使用分析函数使用的比较频繁.所以排序量可能比上一种要多..

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给了我这个答案, 顿时觉得耳目一新啊..就贴出来与大家一起共享了^_^.



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>
jametong 发表于:2005.04.27 21:31 ::分类: ( Oracle tips ) ::阅读:(3587次) :: 评论 (23)
re: 如何sql查询出连续号码段 [回复]

在Table 50000 行的时候性能似乎很差……
参考
http://rollingpig.itpub.net/post/81/42842

rollingpig 评论于: 2006.09.27 13:18
re: 如何sql查询出连续号码段 [回复]

在Table 50000 行的时候性能似乎很差……
参考
http://rollingpig.itpub.net/post/81/42842

rollingpig 评论于: 2006.09.27 13:18
re: 如何sql查询出连续号码段 [回复]

存在
select * from t order by fphm,kshm 无法避免了吧

不过思路还是很好

foreverlee 评论于: 2006.09.27 13:51
re: 如何sql查询出连续号码段 [回复]

不需要order by 执行没问题!

leon 评论于: 2006.10.31 10:10
ejkl wbzckr [回复]

yjrt cxlong xlrpujtm ouryzkdq eqmrkt eckblgi gcijb

faqrux alxz 评论于: 2007.04.07 04:25
wfyhl zxjp [回复]

ivjcmzwsf amgwn xbphotlr pfelxq ycgbxu pmzfnhj kylowha http://www.nawjmru.ujkevo.com

jawupmc xscmi 评论于: 2007.04.07 04:25
qstgbin wtgluzad [回复]

lpgdiruc tofg afyjdxr ixmeskrt ojzrdigs hezoq ciatd upbmijr kwzeb

qewa fvcnrpgiy 评论于: 2007.04.07 04:26
re: 如何sql查询出连续号码段 [回复]

web hosting

free web hosting 评论于: 2007.05.22 01:24
re: 如何sql查询出连续号码段 [回复]

home version of jenny cr

home version of jenny cr 评论于: 2007.05.27 14:17
dpzppmgsxy [回复]

okpnf

tbgtagzzxb 评论于: 2007.05.29 17:46
bylejgxosm [回复]

[url=http://dhqynhh.com]xybgde[/url]

gkzwqowkmy 评论于: 2007.05.29 17:49
oepkcpxpyp [回复]

http://vdxmxqvl.com

cndlqpwxpp 评论于: 2007.05.29 17:55
wm0k7t sucking the alice's nipples with a rings in them [回复]

wm0k7t sucking the alice's nipples with a rings in them

Apocalypses 评论于: 2008.03.01 22:48
YzKoyG the ruler of bright moon [回复]

YzKoyG the ruler of bright moon

AqWEST 评论于: 2008.03.02 17:27
RujVw6 the current ruler of the kingdom of brightmoon [回复]

RujVw6 the current ruler of the kingdom of brightmoon

aric@us 评论于: 2008.03.03 11:54

tgp

nekd 评论于: 2008.08.29 20:17

adult

nekin 评论于: 2008.08.31 02:26
Aide louisiana national nurse skill test [回复]

Aide louisiana national nurse skill test

Aide 评论于: 2008.09.02 15:17
southwest airlines reservations [回复]

american airlines northwest airlines american airlines tickets american airlines continental airlines southwest airline ticket information southwest airlines boarding pass southwest airlines schedules southwest airline tickets southwest airlines flights southwest airlines ticket purchase southwest airline ticket reservation southwest airlines boarding pass southwest airline tickets buy southwest airline ticket southwest airlines schedules southwest airlines schedules fares southwest airlines reservations southwest airlines schedules manila southwest airline schedule tickets southwest airlines schedules southwest airlines flight schedule southwest airlines schedules fares southwest airlines reservations southwest airline schedule tickets southwest airline ticket information southwest airline ticket prices southwest airlines flights southwest airlines boarding pass southwest airlines flight reservations priceline airfare priceline priceline airline ticket cheap airfare orbitz delta airline ticket prices delta airline ticket prices delta airlines reservations delta airline ticket reservation delta airlines flights dirt cheap airline tickets really cheap airline ticket cheapest airline tickets airline cheap dirt online ticket cheap airline tickets southwest airline southwest airlines flights southwest airline tickets southwest airlines schedules southwest airlines airfares orbitz cheap tickets priceline orbitz flights expedia cheapest discount airline tickets air travel discount airline ticket discount airline tickets discount airline ticket online military discount airline tickets discount airline tickets discount airline ticket online military discount airline tickets cheapest discount airline tickets delta airline ticket discount cheap airline tickets delta airline cheap ticket cheap last minute airline ticket discount airline tickets find cheap airline tickets northwest airlines flight confirmation northwest airlines delta flight confirmation northwest airline ticket confirmation northwest airlines flight information delta airline ticket fares delta airline flight delta airlines flight schedule delta airline flight arrival delta airlines flight tracking delta airline domestic flight schedule delta airline job employment delta airlines united airlines american airlines delta airline tickets delta airline flight delta airline reservation confirmation delta airline flight delta airline tickets delta airline ticket confirmation delta airlines reservations delta airlines job applications delta airlines human resources southwest airline job application delta airlines job openings delta airline job employment united airlines united airlines homepage united airlines mileage plus southwest airlines continental airlines airline jobs southwest airlines jobs delta airlines jobs airline job opportunity southwest airlines job openings cheap flights orbitz cheap airline flights cheap airline tickets cheap flights europe southwest american airlines delta airtran continental discounted southwest airline tickets

awlembw 评论于: 2008.09.05 09:03

eating after dark
dracut after dark
arts visual arts printmaking
hernia sections
download sony acid
andy milonakis as a wwe caw
cd ib100ii power cord pics

eating-after-dark 评论于: 2008.09.05 11:31

eating after dark
dracut after dark
arts visual arts printmaking
hernia sections
download sony acid
andy milonakis as a wwe caw
cd ib100ii power cord pics

download-sony-acid 评论于: 2008.09.05 14:06

beauty norfolk salon virginia
beauty and the beast character
beauty oregon salem store supply
beauty fitness skin treatment
beauty child pageant texas
beauty new orleans store supply
beauty advice for man
beauty pageant message boards
beauty or amp or fitness

beauty-norfolk 评论于: 2008.09.05 16:19

beauty norfolk salon virginia
beauty and the beast character
beauty oregon salem store supply
beauty fitness skin treatment
beauty child pageant texas
beauty new orleans store supply
beauty advice for man
beauty pageant message boards
beauty or amp or fitness

store-supply 评论于: 2008.09.05 18:34

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)




切换风格
新闻聚合
博客日历
文章归档...
最新发表...
博客统计...
网站链接...