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

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

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  
 

.

· 网友精彩回答:

处理 SSI 文件时出错
© 2006-2008 All Rights Reserved