有个表格如下,有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 列值最小的那一行记录.
不知道这样说有没有清楚了.求助
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 列值最小的那一行记录.
不知道这样说有没有清楚了.求助
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
*/
语句貌似有点复杂!
思路是这样:首先找到col2小于零,col4最大的那两条记录,然后就在找到得记录的col2上加1可得到另外的两条记录,思路貌似没太大问题,没有测试!试下!