请教:如何将连续的卡号展示成起始卡号和结尾卡号形式

bingou1982 2013-01-11 02:32:20
假设数据库中表A中“卡号”字段,普通查询:select 卡号 from A 得出结果如下:
8986010860510012102
8986010860510012106
8986010860510012107
8986010860510012108
8986010860510027968
8986010860510027969
8986010860510027970
8986010860511027971
8986010860510027969
8986010860510027970
8986010860511027972
8986010860510027973
希望得出如下结果
起始卡号 结尾卡号 数量
8986010860510012102 8986010860510012102 1
8986010860510012106 8986010860510012108 3
8986010860510027968 8986010860510027973 8
请教各位高手,该如何写啊?


...全文
171 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
善若止水 2013-01-11
  • 打赏
  • 举报
回复
楼主说的起始卡号和结尾卡号该怎么去定义?有没有统一的说明呀
UPC子夜 2013-01-11
  • 打赏
  • 举报
回复
为了方便起见 我把重复的去掉了 楼主的数据如果有重复的 可以再单独处理一下
UPC子夜 2013-01-11
  • 打赏
  • 举报
回复 1
with tab as (select '102' as card from dual union select '106' as card from dual union select '107' as card from dual union select '108' as card from dual union select '968' as card from dual union select '969' as card from dual union select '970' as card from dual union select '971' as card from dual ) select t2.c1, t1.c1, (t1.c1 - t2.c1 + 1) as 数量 from (select c1, c2, rownum xh from (select distinct t.c1, t.c2 from (select distinct card c1, card + 1 as c2 from tab) t start with 1 = 1 connect by prior t.c2 = t.c1 order by t.c1) where c1 in (select card from tab) and c2 not in (select card from tab)) t1, (select c1, c2, rownum xh from (select distinct t.c1, t.c2 from (select distinct card c1, card + 1 as c2 from tab) t start with 1 = 1 connect by prior t.c2 = t.c1 order by t.c1) where c1 in (select card from tab) and c1 - 1 not in (select card from tab)) t2 where t1.xh = t2.xh 说实话,我也有点绕晕了,但总算出来了,楼主先试试效果 可以的话,我再解释
bingou1982 2013-01-11
  • 打赏
  • 举报
回复
跪求高手来解决啊

17,378

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧