我有一個SQL Server中的一個表中有一個巨大的數字範圍。我希望能夠爲特定範圍的值設置顏色。如果它在0到20%之間,它應該是紅色的,然後是21到40%,它應該是藍色的,等等。如何在SQL Server中爲特定範圍的值設置顏色?
我試過一些代碼,但它返回一些NULL值,我不明白爲什麼。我該如何解決?
WITH cte as
(
SELECT
[% APL Population],
([% APL Population]/NULLIF(Max([% APL Population]) OVER(), 0)) * 100 AS [% APL PopulationPercent]
FROM
dbo.Indicators)
SELECT
[% APL Population],
[% APL Populationcolor] =
CASE
WHEN [% APL PopulationPercent] BETWEEN 0 AND 20 THEN 'red'
WHEN [% APL PopulationPercent] BETWEEN 21 AND 40 THEN 'blue'
WHEN [% APL PopulationPercent] BETWEEN 41 AND 60 THEN 'green'
WHEN [% APL PopulationPercent] BETWEEN 61 AND 100 THEN 'purple'
END
FROM cte
預先感謝您。
您錯過了20-21,40-41等值之間的錯誤。使用之間創建間隙,交換到>和< – lad2025