CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  疑难问题

分组中加顺序号

楼主fu_manxing()2006-03-07 16:18:51 在 MS-SQL Server / 疑难问题 提问

表A  
  bm     xm  
  ba       xxx  
  ba       yyy  
  ba       fff  
  bb       dfdf  
  bb       fd  
  bb       fdf  
   
  要求按编码分组加上顺序号  
  结果如下  
   
  序号         bm         xm  
   
  ba001       ba       xxx  
  ba002       ba       yyy  
  ba003       ba       fff  
  bb001       bb       dfdf  
  bb002       bb       fd  
  bb003       bb       fdf  
  问题点数:20、回复次数:5Top

1 楼happyflystone(无枪的狙击手)回复于 2006-03-07 16:22:57 得分 0

select   bm+right('0000'+cast((select   count(1)   from   table   where   bm   =   a.bm   and   xm   >a.xm)   as   varchar(3)),3),bm,xm  
  from   table   aTop

2 楼happyflystone(无枪的狙击手)回复于 2006-03-07 16:26:08 得分 17

declare   @t   table(bm   char(2),   xm   varchar(4))  
  insert   @t  
  select   'ba','xxx'   union   all  
  select   'ba','yyy'   union   all  
  select   'ba','fff'   union   all  
  select   'bb','dfdf'   union   all  
  select   'bb','fd'   union   all  
  select   'bb','fdf'  
   
  select    
  bm+right('0000'+cast((select   count(1)+1   from   @t   where   bm   =   a.bm   and   xm   >a.xm)   as   varchar(3)),3)   as   bbm,  
  bm,xm  
  from   @t   a  
  order   by   bbm  
   
  /*  
   
  bbm             bm       xm        
  --------   ----   ----    
  ba001         ba       yyy  
  ba002         ba       xxx  
  ba003         ba       fff  
  bb001         bb       fdf  
  bb002         bb       fd  
  bb003         bb       dfdf  
   
  */Top

3 楼zhaoanle(zhao)回复于 2006-03-07 16:43:01 得分 1

select    
  a.bm+right('000'+cast((select   count(1)   from   表A   where   xm>=a.xm   and   bm=a.bm)as   varchar(10)),3)   as   bm,  
  bm,xm  
  from   表A   aTop

4 楼mislrb(上班看看早报,上上CSDN,下班看看电影)回复于 2006-03-07 16:47:29 得分 1

declare   @t   table(bm   varchar(2),   xm   varchar(4))  
  insert   @t  
  select   'ba','xxx'   union   all  
  select   'ba','yyy'   union   all  
  select   'ba','fff'   union   all  
  select   'bb','dfdf'   union   all  
  select   'bb','fd'   union   all  
  select   'bb','fdf'  
   
  select   id=identity(int,1,1),*   into   #t   from   @t  
   
  select   id=rtrim(ltrim(bm))+right('000'+cast(id+1-(select   min(id)   from   #t   where   bm=t.bm)   as   varchar(5)),3),t.bm,t.xm  
  from   #t   t  
   
  drop   table   #t  
  Top

5 楼vovo2000(没人要的猫)回复于 2006-03-07 16:48:32 得分 1

declare   @t   table(bm   char(2),   xm   varchar(4))  
  insert   @t  
  select   'ba','xxx'   union   all  
  select   'ba','yyy'   union   all  
  select   'ba','fff'   union   all  
  select   'bb','dfdf'   union   all  
  select   'bb','fd'   union   all  
  select   'bb','fdf'  
   
   
  select    
  T1.bm   +'00'   +convert(varchar(3),count(1))   ,  
  T1.xm  
  from    
  @t   T1   join   @t   T2  
    on   T1.bm   =   T2.bm   and   T1.xm   >=   T2.xm  
  group   by    
  T1.bm,T1.xm  
   
  --------------  
  ba xxx  
  ba yyy  
  ba fff  
  bb dfdf  
  bb fd  
  bb fdf  
   
  ------------  
  bb001 dfdf  
  bb002 fd  
  bb003 fdf  
  ba001 fff  
  ba002 xxx  
  ba003 yyy  
  Top

相关问题

  • 请教,一个SQL的写法——关于分组加顺序号
  • checkbox分组选中
  • datareport 分组报表中,怎样实现 分组统计且在报表注脚中加入不分组统计项?????请大家指点指点!
  • FORM中控件的加载顺序?
  • 求助一个脚本(关于安顺序分组数据的..)
  • 如何在ToolBars中加入竖线分组,想IE风格一样?
  • DW中加分组后显示时有一横线,怎么样去除?
  • 分组报表中的加总问题---在线等待,高手救命
  • 怎样为分组的数据窗口中的每一组加上序号
  • 怎样为分组的数据窗口中的每一组加上序号

关键词

  • bm
  • xm
  • ba
  • bbm
  • fffbb
  • fdbb
  • yyyba
  • xmfrom
  • xxxba
  • bb

得分解答快速导航

  • 帖主:fu_manxing
  • happyflystone
  • zhaoanle
  • mislrb
  • vovo2000

相关链接

  • SQL Server类图书

广告也精彩

反馈

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