所以我有数据列应该对应红色,黄色或绿色。绿色是可达到的最高,黄色在中间,红色是最低的。
我在下面写的内容检查这些列的总和是否为零。如果是,则分配N / A等等。
如果绿色比红色更多,绿色多于黄色,则指定为优秀。但是,这是错误的逻辑,因为如果有3个绿色和3个红色等重复,那么当它应该是黄色时,我的整体输出将对应于绿色。
IF SUM([# of Yellow])=0 and SUM([# of Green])=0 and SUM([# of Red])=0 THEN
"N/A"
elseif SUM([# of Green])> SUM([# of Red]) and SUM([# of Green])> SUM([# of Yellow]) THEN
"Excellent"
elseif SUM([# of Red])> SUM([# of Green]) and SUM([# of Red])> SUM([# of Yellow]) THEN
"Poor"
ELSE
"Fair"
End
此外,如果我想为绿色指定1,将黄色指定为3或黄色指定为5,或者指定得分最高的值,则该颜色将占主导地位。我怎么写这个?
答案 0 :(得分:0)
使用SQL子查询
SELECT IF Y=0 and G=0 and R=0 THEN
"N/A"
elseif G> R and G> Y THEN
"Excellent"
elseif R> G and R> Y THEN
"Poor"
ELSE
"Fair"
End
FROM
(
SELECT Y = SUM([# of Yellow]), G = SUM([# of Green]), R = SUM([# of Red]) FROM ...
) AS A
答案 1 :(得分:0)
您可以使用算法
SELECT
CASE
WHEN SUM([# of Yellow])=0 and SUM([# of Green])=0 and SUM([# of Red])=0 THEN 'N/A'
WHEN ( ( SUM([# of Green]) > SUM([# of Red]) )
OR ( SUM([# of Green]) = SUM([# of Red]) AND SUM([# of Green]) > SUM([# of Yellow]) ) ) THEN 'Excellent'
WHEN ( ( SUM([# of Red]) > SUM([# of Green]) )
OR ( SUM([# of Red]) = SUM([# of Green]) AND SUM([# of Red]) > SUM([# of Yellow]) ) ) THEN 'Poor'
ELSE 'Fair'
End
...
答案 2 :(得分:0)
您可以使用嵌套的case语句。我附上了一个示例临时表供参考。 case语句首先检查sum值是否为0.接下来,如果(Green > Yellow) AND (Green > Red)
它给出权重为Green。类似的逻辑用于Red。默认情况下,最后一种可能性为黄色,除ELSE 'Yellow'
DECLARE @temp TABLE (Red int, Yellow int, Green int)
INSERT INTO @temp
VALUES (0, 0, 0)
, (2, 1, 1)
, (2, 2, 2)
, (1, 2, 3)
, (1, 3, 2)
, (3, 0, 3)
SELECT CASE WHEN Green + Yellow + Red = 0
THEN 'N/A'
ELSE CASE WHEN (Green > Yellow) AND (Green > Red)
THEN 'Green'
ELSE CASE WHEN (Red > Green) AND (Red > Yellow)
THEN 'Red'
ELSE 'Yellow'
END
END
END AS [Weight]
FROM @temp