我有一张表格如下:
Area Request Assign Complete
-----------------------------------------------
area1 66 0 33
area2 33 80 0
area3 72 20 9
area4 3 27 89
根据表格
中行的最大值显示最大值和颜色期望的输出:
Area Max_value Color
-------------------------------
area1 66 Red
area2 80 Blue
area3 72 Red
area4 89 Green
答案 0 :(得分:0)
请尝试以下SELECT
。
SELECT AREA,
CASE WHEN Request > Assign AND Request > Complete THEN Request
WHEN Assign > Request AND Assign > Complete THEN Request
WHEN Complete > Request AND Complete > Assign THEN Request
ELSE NULL END AS Max_Value,
CASE WHEN Request > Assign AND Request > Complete THEN 'Red'
WHEN Assign > Request AND Assign > Complete THEN 'Blue'
WHEN Complete > Request AND Complete > Assign THEN 'Green'
ELSE NULL END AS Color
FROM YourTable
答案 1 :(得分:0)
使用UNION ALL OR UNION,如下所示:
SELECT
Area,
Request AS MaxValue,
'Red' AS Color
FROM TableName
WHERE Request>Assign AND Request>Complete
UNION ALL
SELECT
Area,
Assign AS MaxValue,
'Blue' AS Color
FROM TableName
WHERE Assign>Request AND Assign>Complete
UNION ALL
SELECT
Area,
Complete AS MaxValue,
'Green' AS Color
FROM TableName
WHERE Complete>Request AND Complete>Assign
答案 2 :(得分:0)
您可以使用cross apply
查找最大值。然后找到case
表达式
select
Area, maxVal
, Color = case
when maxVal = Request then 'Red'
when maxVal = Assign then 'Blue'
else 'Green'
end
from
myTable
cross apply (select maxVal = max(v) from (values (Request), (Assign), (Complete)) t(v)) q
答案 3 :(得分:0)
请尝试此解决方案 -
SELECT * , CASE WHEN MaxValue = Request THEN 'Red'
WHEN MaxValue = Assign THEN 'Blue'
WHEN MaxValue = Complete THEN 'Green'
END Color
FROM ty
CROSS APPLY
(
SELECT Max(v) MaxValue
FROM
(
VALUES (Request) , (Assign), (Complete)
) as value(v)
)u
<强>输出强>
Area Request Assign Complete MaxValue Color
---------- ----------- ----------- ----------- ----------- -----
area1 66 0 33 66 Red
area2 33 80 0 80 Blue
area3 72 20 9 72 Red
area4 3 27 89 89 Green
(4 rows affected)
答案 4 :(得分:0)
试试这个:
select Area,
case when Request > Assign then
case when Request > Complete then Request
else Complete end
else
case when Assign > Complete then Assign
else Complete END
end [Max_value],
case when Request > Assign then
case when Request > Complete then 'Red'
else 'Green' end
else
case when Assign > Complete then 'Blue'
else 'Green' END
end [Color]
from MY_TABLE