+ -
当前位置:首页 → 问答吧 → 取小于当前值的最大值的那条记录. 的sql 怎么写

取小于当前值的最大值的那条记录. 的sql 怎么写

时间:2011-12-05

来源:互联网

表A 
id col1 col2 .....

给2个参数 s1,s2(其中一个可能为空)


在 col1 小于 s1 或者 col2 小于 s2 的记录中取出

col1 和 col2 都是最大的一条记录

作者: ayun00   发布时间: 2011-12-05

select max(col1),max(col2) from a where col1<@s1 and col2<@s2

作者: fredrickhu   发布时间: 2011-12-05

SQL code
select top 1 * from tb
where col1<(case when s1 is not null then s1 else col1+1 end)
and col2<(case when s2 is not null then s2 else col2+1 end)
order by col1 desc,col2 desc

两个都是最大的"一条记录"取不出来,因为我不能保证在一条记录里,它俩都是最大值.

作者: qianjin036a   发布时间: 2011-12-05

楼主把结构和数据贴出来,以及你想要的结果

作者: pengxuan   发布时间: 2011-12-05

SQL code
select * from tb
where col1<isnull(s1,col1+1) and col2<isnull(s2,col2+1)
order by col1 desc,col2 desc

作者: qianjin036a   发布时间: 2011-12-05

SQL code
select max(col1),max(col2) from a 
 where col1<isnull(@s1,col1+1) or col2<isnull(s2,col2+1)

作者: ssp2009   发布时间: 2011-12-05