CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  疑难问题

求助sql实现序列号的连续合并..

楼主yosoft()2005-11-29 22:01:05 在 MS-SQL Server / 疑难问题 提问

一个表    
      字段   :snumber(int)  
                  1  
                  2  
                  5  
                  6  
                  7  
                  8  
                  10  
                  12  
   
        用sql转化成  
                1-2,5-8,10,12  
        先谢谢各位大虾!!  
  问题点数:100、回复次数:5Top

1 楼zlp321002(Life Is Good,Let's Shine)回复于 2005-11-29 22:10:51 得分 30

--给你一个Sample,参考下  
  create   table   tb(编号   varchar(10))  
  insert   tb   select   1    
  union   all   select   2  
  union   all   select   5  
  union   all   select   6  
  union   all   select   7  
  union   all   select   8  
  union   all   select   10  
  union   all   select   12  
  go  
   
  --查询处理  
  select   编号=b.编号+'-'+b.编号1  
  from   tb   a,(  
  select   编号,编号1=(select   min(编号)   from   tb   a1  
  where   编号>=a.编号  
  and   not   exists(  
  select   *   from   tb  
  where   编号=a1.编号+1))  
  from   tb   a  
  where   not   exists(  
  select   *   from   tb    
  where   编号=a.编号-1))b  
  where   a.编号   between   b.编号   and   b.编号1  
  group   by   b.编号,b.编号1  
  go  
   
  --删除测试  
  drop   table   tb  
   
   
  --结果  
  编号                                          
  ---------------------    
  1-10  
  10-10  
  12-12  
  5-8  
   
  (所影响的行数为   4   行)  
  Top

2 楼pbsql(风云)回复于 2005-11-29 22:23:54 得分 20

create   table   #t(snumber   int)  
  insert   #t  
  select                 1   union   all  
  select                 2   union   all  
  select                 5   union   all  
  select                 6   union   all  
  select                 7   union   all  
  select                 8   union   all  
  select                 10   union   all  
  select                 12  
   
  select   numfrom=snumber,  
  numto=(  
  select   min(snumber)   from   #t   b  
  where   not   exists(select   1   from   #t   where   snumber=b.snumber+1)  
              and   b.snumber>=a.snumber)  
  from   #t   a  
  where   not   exists(select   1   from   #t   where   snumber=a.snumber-1)  
  drop   table   #tTop

3 楼pbsql(风云)回复于 2005-11-29 22:24:31 得分 0

返回:  
  1 2  
  5 8  
  10 10  
  12 12  
  Top

4 楼samfeng_2003(凤翼天翔)回复于 2005-11-29 22:27:22 得分 50

create   table   t   (snumber   int)  
   
  insert   t  
  select   1   union   all  
  select   2   union   all  
  select   5   union   all  
  select   6   union   all  
  select   7   union   all  
  select   8   union   all  
  select   10   union   all  
  select   12    
   
  select   id=identity(int),snumber   into   #1   from   t   a  
  where   not   exists  
  (select   *   from   t   where   snumber=a.snumber-1)  
  select   id=identity(int),snumber   into   #2   from   t   a  
  where   not   exists  
  (select   *   from   t   where   snumber=a.snumber+1)  
  select   col=case   when   a.snumber<>b.snumber   then   cast(a.snumber   as   varchar)+'-'+cast(b.snumber   as   varchar)  
  else   cast(a.snumber   as   varchar)   end  
  into   #3   from   #1   a,#2   b  
  where   a.id=b.id  
   
  declare   @col1   varchar(100)  
  update   #3   set  
  @col1=case   when   @col1   is   null   then   col   else   @col1+','+col   end,  
  col=@col1  
   
  select   max(col)   as   col   from   #3  
   
  drop   table   #1,#2,#3  
  drop   table   t  
   
   
  col                                                                                                                        
  -------------------------------------------------------------    
  1-2,5-8,10,12  
   
  (所影响的行数为   1   行)  
  Top

5 楼yosoft()回复于 2005-12-09 15:25:09 得分 0

不好意思,一直在忙,谢谢各位拉!!Top

相关问题

  • sql server 7.0的序列号???
  • SQL Server 2000 的序列号???
  • Sybase SQL Anywhere 7.0的安装序列号谁知道?
  • 有人知道正版Sql Server的序列号吗? 急!!!!!!!
  • 急求SQL Server2000中文版的序列号!!!
  • 有谁知道SQL Server2000的安装序列号?谢谢
  • SQL SERVER2000中文标准版的序列号
  • 请问一下sql server 2000的企业版的序列号???
  • SQL Server 2000的序列号有谁知道?
  • 急需SQL Server 2000的序列号!谢谢.

关键词

  • snumber
  • 编号
  • allselect
  • union
  • col
  • tb
  • varchar
  • cast
  • drop
  • table

得分解答快速导航

  • 帖主:yosoft
  • zlp321002
  • pbsql
  • samfeng_2003

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo