求教连续号段合并的SQL。

[复制链接]
查看11 | 回复9 | 2009-10-9 08:28:00 | 显示全部楼层 |阅读模式
比如号段表信息如下:
kshm jshm
1230012399
1240012410
12510 12560
1257012599
希望得到以下结果
kshm jshm
12300 12410
1251012560
1257012599
也就是合并连续的号段。
谢谢
回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
12300 12410
这也是同一号段?楼主,有错误吧.
回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
只要连续都算。
回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
我总结过这样的如何处理,你搜索下吧,给你个例子!
[PHP]sql
SQL> With x As
2(
3Select '002' eba002,'001' eba083,'100' eba084 From dual
4Union
5Select '002','101','200' From dual
6Union
7Select '002','201','250' From dual
8Union
9Select '002','301','400' From dual
10Union
11Select '003','001','200' From dual
12Union
13Select '003','300','400' From dual
14)
15Select Distinct eba002,decode(a,1,Min(eba083) over (Partition By eba002,a),eba083),
16decode(a,1,max(eba084) over (Partition By eba002,a),eba084) From (
17Select eba002,eba083,eba084,
18eba083 - lag(eba084,1,eba083-1) over (Partition By eba002 Order By eba083) a
19From x
20) order by 1,2
21/
EBA002 DECODE(A,1,MIN(EBA083)OVER(PAR DECODE(A,1,MAX(EBA084)OVER(PAR
------ ------------------------------ ------------------------------
002001
250
002301
400
003001
200
003300
400
SQL>
[/PHP]
回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
谢谢hanjs,呵呵。
不过我给出来的号段其实是没有固定的号段分组,只要是连续就要合并成一个较大的号段。
对于这点,现在还不是很理解。
还请大侠们多多指教 。
回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
你改改我的sql就可以了!


回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
[PHP]你应该自己动手的
SQL> With x As (
2Select 12300 kshm , 12399 jshm From dual
3Union All
4Select 12400, 12410 From dual
5Union All
6Select 12510, 12560 From dual
7Union All
8Select 12570, 12599 From dual
9)
10Select Distinct decode(a,1,Min(kshm) over (Partition By a),kshm),
11decode(a,1,max(jshm) over (Partition By a),jshm) From (
12Select kshm,jshm,
13kshm - lag(jshm,1,kshm-1) over (Order By kshm) a
14From x
15) order by 1,2
16/
DECODE(A,1,MIN(KSHM)OVER(PARTI DECODE(A,1,MAX(JSHM)OVER(PARTI
------------------------------ ------------------------------

12300
12410

12510
12560

12570
12599
SQL>

[/PHP]
回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
3q。不过如果表数据如下呢:
kshm
jshm
12300
12399
12400
12410
12510
12560
12570
12579
12580
12589
12590
12599
要求的结果如下
12300 12410
12510 12560
12570 12599
我试了很多办法,都没有办法定位窗口的范围。
回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
最初由 hanjs 发布
[B][PHP]你应该自己动手的
SQL> With x As (
2Select 12300 kshm , 12399 jshm From dual
3Union All
4Select 12400, 12410 From dual
5Union All
6Select 12510, 12560 From dual
7Union All
8Select 12570, 12599 From dual
9)
10Select Distinct decode(a,1,Min(kshm) over (Partition By a),kshm),
11decode(a,1,max(jshm) over (Partition By a),jshm) From (
12Select kshm,jshm,
13kshm - lag(jshm,1,kshm-1) over (Order By kshm) a
14From x
15) order by 1,2
16/
DECODE(A,1,MIN(KSHM)OVER(PARTI DECODE(A,1,MAX(JSHM)OVER(PARTI
------------------------------ ------------------------------

12300
12410

12510
12560

12570
12599
SQL>

[/PHP] [/B]





回复

使用道具 举报

千问 | 2009-10-9 08:28:00 | 显示全部楼层
典型的为中国移动通信,中国联通通信计算那些号码已经在使用了,那些号段还可以继续放号阿,重新分配资源
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行