if object_id('t1') is not null drop table t1 go create table t1 ( i int identity(1,1) ,p char(3) ,w int ,sn int ,so int ,sl int ,rt money ) go
INSERT INTO t1 VALUES('AIR',1,5,20,111,10.11) INSERT INTO t1 VALUES('AIR',1,6,21,111,20.34) INSERT INTO t1 VALUES('AIR',2,7,23,111,30.22) INSERT INTO t1 VALUES('AIR',2,8,23,111,40.12) INSERT INTO t1 VALUES('PID',1,9,20,110,10.56) INSERT INTO t1 VALUES('PID',1,10,20,111,10.57) INSERT INTO t1 VALUES('PID',2,11,20,111,10.44) INSERT INTO t1 VALUES('PID',2,12,20,111,10.34) INSERT INTO t1 VALUES('ZLM',1,13,20,111,10.22) INSERT INTO t1 VALUES('ZLM',1,14,20,111,20.45) INSERT INTO t1 VALUES('ZLM',2,15,20,111,10.67) INSERT INTO t1 VALUES('ZLM',2,16,20,111,20.89)
go
select t.i ,t.p ,t.w ,t.sn ,t.so ,t.sl ,t.rt from( select t.i ,t.p ,t.w ,t.sn ,t.so ,t.sl ,t.rt from( select t.i ,t.p ,t.w ,t.sn ,t.so ,t.sl ,t.rt from t1 t where so IN (select min(so) from t1 where p=t.p and w=t.w) group by t.i ,t.p,t.w ,t.sn ,t.so ,t.sl ,t.rt ) a join t1 t on t.i=a.i where t.sl IN (select min(sl) from t1 where p=t.p and w=t.w) group by t.i ,t.p,t.w ,t.sn ,t.so ,t.sl ,t.rt ) a join t1 t on t.i=a.i where t.rt IN (select max(rt) from t1 where p=t.p and w=t.w) group by t.i ,t.p,t.w ,t.sn ,t.so ,t.sl ,t.rt |