select max(a) from tb1 where a < ( select max(a) from tb1 )
select top 1 * from tablename where col not in (select Max(col) from tablename) order by col desc
select top 1 a.* from (select top 2 * from table order by ID desc) as a order by a.ID
select a from 表 a where(select count(*) from 表 where a>=a.a)=2 --第二大 select a from 表 a where(select count(*) from 表 where a>=a.a)=3 --第三大select a from 表 a where(select count(*) from 表 where a>=a.a)=n --第N大
邹建的方法在大数据量的表中查询速度太慢(无法忍受),我献一条(在100多万条的收费表中速度不超过1秒)select min(a) from (select distinct top n a from 表 order by a desc) t (第n大的)
select max(a) as a from table where a < ( select max(a) from table )
to : sanmaotuo(老冯)烦请在你的100多万条的收费表中测试一下楼上 sankis() 的方法,看看效率如何?
如果有重復的就這樣:select a from 表 a where(select count(Distinct a) from 表 where a>=a.a)=n --第N大不過卻很慢 不知可以怎樣優化?
你们在写top的时候考虑了有a的数值是一样的纪录了吗??????不用写with tie???
TO :jackxing(Jack) 测试了。速度很快。可惜SANKIS()的语句只能计算第2大的,不通用
to: woshishui11121113(我是谁) 老冯写的已经DISTINCT了。
to : sanmaotuo(老冯)没错,还是你的方法更高一筹,谢了! 真羡慕你,有100多万条数据可用,呵呵
to : sanmaotuo(老冯)没错,还是你的方法更高一筹,谢了! 真羡慕你,有100多万条数据可用,呵呵
(select top 2 * from table
order by ID desc) as a
order by a.ID
where(select count(*) from 表 where a>=a.a)=2 --第二大
select a from 表 a
where(select count(*) from 表 where a>=a.a)=3 --第三大select a from 表 a
where(select count(*) from 表 where a>=a.a)=n --第N大
(第n大的)
where(select count(Distinct a) from 表 where a>=a.a)=n --第N大不過卻很慢
不知可以怎樣優化?
老冯写的已经DISTINCT了。
真羡慕你,有100多万条数据可用,呵呵
真羡慕你,有100多万条数据可用,呵呵