有个表格如下,有xmjc,      col2   ,    col3  ,   col4 ,数据如下:xmjc       col2       col3       col4
a          100         1          00-01
a          200         2          00-02
a          150         3          00-03
a          -100        4          00-04
a          -100        5          00-05
a          100         6          00-06
a          130         7          00-07
a          150         8          00-08
a          -100        9          00-09
a          130         10         00-10
a          150         11         00-11
b          -100        5          00-05
b          100         6          00-06
b          130         7          00-07
b          150         8          00-08
b          120         9          00-09
b          -100        10         00-10
b          150         11         00-11
b          130         12         00-12想得到的结果是:a          130         10         00-10
b          150         11         00-11
即:对于不同xmjc, 得到其倒序排序的 col2>=0  的 col3 列值最小的那一行记录.
 
不知道这样说有没有清楚了.求助

解决方案 »

  1.   

    简单的说....就是要得到 col2 "开始" >=0   的记录比如col2 列的值为 -1 , 1 ,-2 ,2 ,-3,3,4,5,7....这样的话我就需要得到col2 的值为3 的那条记录或者大伙能不能帮我想个其他办法....
      

  2.   

    当然,还有需要根据xmjc分组。
      

  3.   

    create table tb(xmjc varchar(10),col2 int,col3 int,col4 varchar(10))
    insert into tb select 'a',  100,1,'00-01' 
    insert into tb select 'a',  200,2,'00-02' 
    insert into tb select 'a',  150,3,'00-03' 
    insert into tb select 'a',  -100,4,'00-04' 
    insert into tb select 'a',  -100,5,'00-05' 
    insert into tb select 'a',  100,6,'00-06' 
    insert into tb select 'a',  130,7,'00-07' 
    insert into tb select 'a',  150,8,'00-08' 
    insert into tb select 'a',  -100,9,'00-09' 
    insert into tb select 'a',  130,10,'00-10' 
    insert into tb select 'a',  150,11,'00-11' 
    insert into tb select 'b',  -100,5,'00-05' 
    insert into tb select 'b',  100,6,'00-06' 
    insert into tb select 'b',  130,7,'00-07' 
    insert into tb select 'b',  150,8,'00-08' 
    insert into tb select 'b',  120,9,'00-09' 
    insert into tb select 'b',  -100,10,'00-10' 
    insert into tb select 'b',  150,11,'00-11' 
    insert into tb select 'b',  130,12,'00-12' 
    go
    select t1.* from tb t1 inner join (
    select xmjc, min(col4)as col4 from tb a where col4>(
    select top 1 col4 from tb where xmjc=a.xmjc and col2<0 order by col4 desc
    ) group by xmjc
    ) t2 on t1.xmjc=t2.xmjc and t1.col4=t2.col4
    go
    drop table tb
    /*
    xmjc       col2        col3        col4
    ---------- ----------- ----------- ----------
    a          130         10          00-10
    b          150         11          00-11
    */
      

  4.   

    select * from MyTable in (select (col2 + 1) as col2 from MyTable,(select xmjc,max(col4) as col4 from MyTable group by xmjc where col2 < 0) as a where MyTable.xmjc = a.xmjc and MyTable.col4 = a.xmjc) as b 
    语句貌似有点复杂!
    思路是这样:首先找到col2小于零,col4最大的那两条记录,然后就在找到得记录的col2上加1可得到另外的两条记录,思路貌似没太大问题,没有测试!试下!
      

  5.   

    qianjin036a  正解...非常感谢楼上各位达人啊.