Sql server 选择最高值,无论何时多次出现相同的值
我使用下面的代码从我的国家1-5中检索最高值,但是当有多个国家具有相同值时,该代码会呈现NULL。如何选择a)国家1-5的最高值,以及B)如果某些国家的值相同,则选择第一个具有该值的国家(从1开始)。 这是我使用的代码:Sql server 选择最高值,无论何时多次出现相同的值,sql-server,Sql Server,我使用下面的代码从我的国家1-5中检索最高值,但是当有多个国家具有相同值时,该代码会呈现NULL。如何选择a)国家1-5的最高值,以及B)如果某些国家的值相同,则选择第一个具有该值的国家(从1开始)。 这是我使用的代码: , Risk AS( SELECT *, (SELECT MAX(v) FROM ( VALUES (Redflag.[country1]), (Redflag.[co
, Risk AS(
SELECT *,
(SELECT MAX(v)
FROM (
VALUES (Redflag.[country1]),
(Redflag.[country2]),
(Redflag.[country3]),
(Redflag.[country4]),
(Redflag.[country5])) AS value(v)
) AS highest_risk
FROM Redflag
)
这就是我得到的:
SELECT 1 AS ID, 0.5 AS country1, 0 AS country2, 0.5 AS country3, 0 AS country4, 0 AS country5, NULL AS highest_risk
UNION
SELECT 2 AS ID, 0.5 AS country1, 0.5 AS country2, 2 AS country3, 0 AS country4, 0 AS country5, 2 AS highest_risk
UNION
SELECT 3 AS ID, 0 AS country1, 2 AS country2, 0.5 AS country3, 0.5 AS country4, 0.5 AS country5, 2 AS highest_risk
UNION
SELECT 4 AS ID, 0 AS country1, 0 AS country2, 0 AS country3, 0 AS country4, 5 AS country5, 5 AS highest_risk
UNION
SELECT 5 AS ID, 0.5 AS country1, 0.5 AS country2, 3 AS country3, 3 AS country4, 0.5 AS country5, NULL AS highest_risk
UNION
SELECT 6 AS ID,0 AS country1, 0 AS country2, 0 AS country3, 0.5 AS country4, 0.5 AS country5, NULL AS highest_risk
UNION
SELECT 7 AS ID, 0 AS country1, 0.5 AS country2, 0.5 AS country3, 0.5 AS country4, 0.5 AS country5, 0.5 AS highest_risk
UNION
SELECT 8 AS ID, 0.5 AS country1, 0.5 AS country2, 0.5 AS country3, 0.5 AS country4, 0.5 AS country5, NULL AS highest_risk
UNION
SELECT 9 AS ID, 0.5 AS country1, 0.5 AS country2, 0.5 AS country3, 0.5 AS country4, 4 AS country5, 4 AS highest_risk
我需要这样:
SELECT 1 AS ID, 0.5 AS country1, 0 AS country2, 0.5 AS country3, 0 AS country4, 0 AS country5, 0.5 AS highest_risk
UNION
SELECT 2 AS ID, 0.5 AS country1, 0.5 AS country2, 2 AS country3, 0 AS country4, 0 AS country5, 2 AS highest_risk
UNION
SELECT 3 AS ID, 0 AS country1, 2 AS country2, 0.5 AS country3, 0.5 AS country4, 0.5 AS country5, 2 AS highest_risk
UNION
SELECT 4 AS ID, 0 AS country1, 0 AS country2, 0 AS country3, 0 AS country4, 5 AS country5, 5 AS highest_risk
UNION
SELECT 5 AS ID, 0.5 AS country1, 0.5 AS country2, 3 AS country3, 3 AS country4, 0.5 AS country5, 3 AS highest_risk
UNION
SELECT 6 AS ID,0 AS country1, 0 AS country2, 0 AS country3, 0.5 AS country4, 0.5 AS country5, 0.5 AS highest_risk
UNION
SELECT 7 AS ID, 0 AS country1, 0.5 AS country2, 0.5 AS country3, 0.5 AS country4, 0.5 AS country5, 0.5 AS highest_risk
UNION
SELECT 8 AS ID, 0.5 AS country1, 0.5 AS country2, 0.5 AS country3, 0.5 AS country4, 0.5 AS country5, 0.5 AS highest_risk
UNION
SELECT 9 AS ID, 0.5 AS country1, 0.5 AS country2, 0.5 AS country3, 0.5 AS country4, 4 AS country5, 4 AS highest_risk
最简单的方法是什么?类似的方法。“最大风险”列包含a)国家1-5的最高值。“min_country”列包含b)如果某些国家具有相同的值,则选择第一个具有该值的国家(从1开始) 资料 质疑 输出
id country1 country2 country3 country4 country5 min_country max_risk
1 0.5 0.0 0.5 0.0 0.0 1 0.5
2 0.5 0.5 2.0 0.0 0.0 3 2.0
3 0.0 2.0 0.5 0.5 0.5 2 2.0
4 0.0 0.0 0.0 0.0 5.0 5 5.0
5 0.5 0.5 3.0 3.0 0.5 3 3.0
6 0.0 0.0 0.0 0.5 0.5 4 0.5
7 0.0 0.5 0.5 0.5 0.5 2 0.5
8 0.5 0.5 0.5 0.5 0.5 1 0.5
9 0.5 0.5 0.5 0.5 4.0 5 4.0
您的查询应该满足您的要求。可能您没有针对这个精确的样本数据运行这个精确的查询。它是CTE的一部分,是否应该更接近数据源?。结果是您希望与现有查询一起使用的结果。
with country_cte(id, country1, country2, country3, country4, country5, v_n, v_v, rn) as (
select *, row_number() over (partition by id order by v.v desc, v.n) rn
from
#redflag r
cross apply
(values (1, r.[country1]), (2, r.[country2]),
(3, r.[country3]), (4, r.[country4]),
(5, r.[country5])) AS v(n, v))
select id, country1, country2, country3, country4, country5, v_n min_country, v_v max_risk
from country_cte
where rn=1;
id country1 country2 country3 country4 country5 min_country max_risk
1 0.5 0.0 0.5 0.0 0.0 1 0.5
2 0.5 0.5 2.0 0.0 0.0 3 2.0
3 0.0 2.0 0.5 0.5 0.5 2 2.0
4 0.0 0.0 0.0 0.0 5.0 5 5.0
5 0.5 0.5 3.0 3.0 0.5 3 3.0
6 0.0 0.0 0.0 0.5 0.5 4 0.5
7 0.0 0.5 0.5 0.5 0.5 2 0.5
8 0.5 0.5 0.5 0.5 0.5 1 0.5
9 0.5 0.5 0.5 0.5 4.0 5 4.0