当前位置:首页 > 技术问答集
firefox

如何将表中的一个列转换为字符串,如果出现连号,则用"-"分隔,不是连号,就用","分隔.比如:0001-0004,009

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

表的内容:  
  列1                     列2  
  0001                   a  
  0002                   a  
  0003                   a  
  0004                   a  
  0009                   a          
   
  急!!!!  
 

· 网友精彩回答:

发表者:zlp321002

--邹老大,以前写过.我改下,不知道楼主是不是要这个效果  
  --示例数据  
  create   table   tb(列号   char(4),值   char(1))  
  insert   tb   select   0001,a      
  union   all   select   0002,a    
  union   all   select   0003,a    
  union   all   select   0004,a    
  union   all   select   0009,a  
  --查询处理  
  select   列号=b.列号+..+b.列号1,值=max(a.值)  
  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  
   
  --结果  
    列号                   值          
  ----------   ----    
  0001..0004   a  
  0009..0009   a  
   
   
  --删除测试环境  
  drop   table   tb  
   
 

.
© 2006-2008 All Rights Reserved