2011-04-28 99 views
0

我在表中有兩列。如何選擇範圍內的值

Percentage, Commission 
100   20 
98   19.5 
97.5   19 
96.25   18.5 
95   18 
... 
80   15 
  • 如果我的百分比是95.25,則佣金應爲18

  • 如果我的百分比是97.5,則佣金應該是19

我該怎麼辦這在SQL Server中?

+0

什麼是實際的規則?在百分比<= @GivenPercentage?行中選擇最大佣金值?或者從百分比最大百分比<= @GivenPercentage的行中選擇Commision? – 2011-04-29 04:25:16

回答

1
SELECT MAX(Commission) FROM #test 
WHERE Percentage <= 96.25 
+0

這是比我的更好的解決方案。希望我會想到:) – Nik 2011-04-28 19:27:26

+0

感謝您的答覆。 – user702268 2011-04-28 20:08:58

1
select top 1 Commission 
from theTable 
where Percentage <= 96.25 
order by Percentage desc 
0

僅僅是爲了提供另一種選擇的緣故:

WITH ranked AS (
    SELECT 
    *, 
    rank = CASE 
     WHEN Percentage > @MaxPercentage THEN 0 
     ELSE ROW_NUMBER() OVER (ORDER BY Percentage DESC) 
    END 
    FROM atable 
) 
SELECT Commission 
FROM ranked 
WHERE rank = 1 

正如我在你的問題評論說,這是不完全清楚哪一行實際上應該提供正確的輸出,最大Percntage或最大Commission的那個。基於此,您可能需要用ORDER BY Commission DESC替換ORDER BY Percentage DESC