2017-10-21 76 views
0

因此,我有一些數據列應該對應於紅色,黃色或綠色。綠色是最高的,黃色是中等,紅色是最低的。爲每種顏色指定權重

我在下面寫的檢查這些列的總和是否爲零。如果是,則分配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

使用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 
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 
... 
0

您可以使用嵌套的case語句。我附上了一個示例臨時表供參考。 case語句首先檢查總和值是否爲0.接下來,如果(Green > Yellow) AND (Green > Red)它給出重量Green。類似的邏輯用於紅色。最後一種可能性是默認情況下爲黃色,並且代碼實際上不需要,除了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 
+0

Tableau將如何寫入? – Nusc

+0

我不知道,對不起。 – Zorkolot