日期:2014-05-17 浏览次数:20534 次
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
*/
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