27,579
社区成员
发帖
与我相关
我的任务
分享
create table test(idx int, freq float)
insert into test values(1,10)
insert into test values(2,11.2)
insert into test values(3,18.4)
insert into test values(4,47.4)
insert into test values(5,50.9)
insert into test values(6,85.7)
DECLARE @num float
set @num=18
select top 1 *
from test
group by idx,freq
order by MIN(abs(freq-@num))
WITH a1 (IDX,FREQ) AS
(
SELECT 1,10 UNION all
SELECT 2,11.2 UNION all
SELECT 3,18.4 UNION all
SELECT 4,47.4 UNION all
SELECT 5,50.9 UNION all
SELECT 6,85.7
)
SELECT TOP 1 IDX
FROM a1
ORDER BY ABS(FREQ-18) asc
declare @T table([IDX] int,[FREQ] float)
insert @T
select 1,10 union all
select 2,11.2 union all
select 3,18.4 union all
select 4,47.4 union all
select 5,50.9 union all
select 6,85.7
DECLARE @freq INT
SET @freq=18
select TOP 1 * from @T ORDER BY ABS([FREQ]-@freq)
/*
IDX FREQ
----------- ----------------------
3 18.4
*/