当前位置:首页 » 软件行业

请高手帮我看一个sql语句(有点难度),不胜感激,等待中!!!!!

八月 3, 2007

 发表在: java



create   table   test(id   varchar(12),   number   varchar(5),class   varchar(4),pieceid   varchar(4))  
  insert   into   test  
                      select   20050613001, 0, 甲,  
  union   all   select   20050613002, 10.5, 甲,  
  union   all   select   20050613003, 30, 甲,  
  union   all   select   20050613004, 35, 甲,  
  union   all   select   20050613005, 40.8, 甲,  
  union   all   select   20050613006, 48, 甲,  
  union   all   select   20050613007, 52.4, 甲,  
  union   all   select   20050613008, 60, 甲,  
  union   all   select   20050613009, 61, 甲,  
  union   all   select   20050613010, 89, 甲,  
  union   all   select   20050613011, 89.9, 甲,  
  union   all   select   20050613012, 90, 甲,  
  union   all   select   20050613013, 91.2, 甲,  
  union   all   select   20050613014, 100, 甲,  
  union   all   select   20050613015, 103.5, 甲,  
  union   all   select   20050613016, 107.5, 甲,  
  union   all   select   20050613017, 119.5, 甲,  
   
  布匹检验时,每个班次都有一个开始,结束的计数值(中间还有其他计数值)  
  在一个班次内,一般从开始值往后每30m(<=30)为一个匹长,为每一个匹长加一个匹号,  
  即   开始     结束   匹号  
        0     -       30       1,  
      30     -       60       2,  
      60     -       90       3,  
      若最后一个匹长<18,则匹号为前一个匹号,  
      若最后一个匹长>=18,则匹号为下一个匹号  
  sql语句如下:  
  select   id,   number,   class,  
                    pieceid   =  
                        cast    
                        (case    
                                when   number   =   0   then   1  
                                when   cast(cast(number   as   float)   as   int)   /   30   =   (cast(number   as   float)   +   0.0)/   30  
                                          then   cast(number   as   float)   /   30  
                                else       cast(number   as   float)   /   30   +   1  
                        end     as   int)                                
    into   #t   from   test   a  
   
  update     test   set   test.pieceid=#t.pieceid     from   #t   where     test.id=#t.id  
  select   *   from   test  
  drop   table   #t  
   
  我在算最后一个pieceid时继续不下去了  
  (先选pieceid最大的数据,如果max(number)/30<18,则pieceid=max(pieceid)-1  
  否则pieceid=max(pieceid))  
   
  想得到的结果如下  
  id                           number     class       pieceid  
  20050613001 .0 甲 1  
  20050613002 10.5 甲 1  
  20050613003 30.0 甲 1  
  20050613004 35.0 甲 2  
  20050613005 40.8 甲 2  
  20050613006 48.0 甲 2  
  20050613007 52.4 甲 2  
  20050613008 60.0 甲 2  
  20050613009 61.0 甲 3  
  20050613010 89.0 甲 3  
  20050613011 89.9 甲 3  
  20050613012 90.0 甲 3  
  20050613013 91.2 甲 4  
  20050613014 100.0 甲 4  
  20050613015 103.5 甲 4  
  20050613016 107.5 甲 4  
  20050613017 119.5 甲 4  
 

.

电脑软件技术推荐:

  • JSF是DOA吗?[详细内容]
  • 意法半导体加入中国开放源计算启动计划[详细内容]
  • 基础与细节[详细内容]
  • Microsoft紧急安全公告(2004年10月)[详细内容]
  • 部分ADSL猫的IP和密码[详细内容]
  • aboutface2.0读书笔记——第一章:目标导向设计[详细内容]
  • Linux下C语言编程--基础知识[详细内容]
  • 如何接收OutLook中不安全附件[详细内容]
  • netstat的一些常用选项[详细内容]
  • Connectionresetbypeer的原因[详细内容]
  • 相关提议:

    No comments in this entry


    Post a Comment »

    Logged in

    Advertising

    Categories

    相关文章