select * from tb order by djbh , sno     ?

解决方案 »

  1.   

    --知道了,这样
    select djbh , mxdh , sno = (select count(1) from tb where djbh = t.djbh and mxdh > t.mxdh) from tb t
      

  2.   

    --上面还错了点,这个对了.
    create table tb(djbh varchar(20), mxdh varchar(20), sno int)
    insert into tb values('060011211600' , '26' , 3 )
    insert into tb values('060011211600' , '25' , 2 )
    insert into tb values('060011211600' , '22' , 1 )
    insert into tb values('060011211600' , '02' , 5 )
    insert into tb values('060011211600' , '01' , 4 )
    insert into tb values('060031211500' , '26' , 3 )
    insert into tb values('060031211500' , '25' , 2 )
    insert into tb values('060031211500' , '22' , 1 )
    insert into tb values('060031211500' , '03' , 6 )
    insert into tb values('060031211500' , '02' , 5 )
    insert into tb values('060031211500' , '01' , 4 )
    goselect djbh , mxdh , sno = (select count(1) from tb where djbh = t.djbh and mxdh > t.mxdh) + 1 from tb t order by djbh , snodrop table tb/*
    djbh                 mxdh                 sno         
    -------------------- -------------------- ----------- 
    060011211600         26                   1
    060011211600         25                   2
    060011211600         22                   3
    060011211600         02                   4
    060011211600         01                   5
    060031211500         26                   1
    060031211500         25                   2
    060031211500         22                   3
    060031211500         03                   4
    060031211500         02                   5
    060031211500         01                   6(所影响的行数为 11 行)
    */
      

  3.   

    declare @t table(djbh varchar(15),mxdh varchar(10),sno int )        
    -------------------------------- -------------------------------- ----------- 
    insert @t select '060011211600'              ,      '26' ,                             3 
    insert @t select '060011211600'              ,      '25' ,                             2 
    insert @t select '060011211600'              ,      '22' ,                             1 
    insert @t select '060011211600'              ,      '02' ,                             5 
    insert @t select '060011211600'              ,      '01' ,                             4 
    insert @t select '060031211500'              ,      '26' ,                             3 
    insert @t select '060031211500'              ,      '25' ,                             2 
    insert @t select '060031211500'              ,      '22',                              1 
    insert @t select '060031211500'              ,      '03',                              6 
    insert @t select '060031211500'              ,      '02',                              5 
    insert @t select '060031211500'              ,      '01',                              4 
    select djbh ,mxdh,(select count(*)+1 from @t t1 where t1.djbh=t2.djbh and t1.mxdh >t2.mxdh )as 排名 from @t t2 order by djbh ,mxdh desc
    /*
    djbh            mxdh       排名          
    --------------- ---------- ----------- 
    060011211600    26         1
    060011211600    25         2
    060011211600    22         3
    060011211600    02         4
    060011211600    01         5
    060031211500    26         1
    060031211500    25         2
    060031211500    22         3
    060031211500    03         4
    060031211500    02         5
    060031211500    01         6*/
      

  4.   

    update tb set sno = (select count(1) from tb where djbh = t.djbh and mxdh >= t.mxdh)  from tb tselect * from tb/*
    djbh                 mxdh                 sno         
    -------------------- -------------------- ----------- 
    060011211600         26                   1
    060011211600         25                   2
    060011211600         22                   3
    060011211600         02                   4
    060011211600         01                   5
    060031211500         26                   1
    060031211500         25                   2
    060031211500         22                   3
    060031211500         03                   4
    060031211500         02                   5
    060031211500         01                   6(所影响的行数为 11 行)
    */