我在选择查询中有以下值,
"IND"
"UK"
"FR"
情况1:如果我有“ IND”,“ UK”和“ FR”,则只需返回
"IND"
"EU"
案例2:如果我有“ IND”和“ UK”,则需要返回为
"IND"
情况3:如果我有“ IND”和“ FR”,则需要返回为
"IND"
注意:如果我只有“ UK”和“ FR”,则必须考虑该值。
答案 0 :(得分:2)
出现 可以得到您想要的答案,但我怀疑它是“最佳”的(并且缩放比例非常大):
WITH CTE AS(
SELECT V.CC,
COUNT(CASE V.CC WHEN 'Ind' THEN 1 END) OVER () AS Ind,
COUNT(CASE V.CC WHEN 'UK' THEN 1 END) OVER () AS UK,
COUNT(CASE V.CC WHEN 'FR' THEN 1 END) OVER () AS FR
FROM (VALUES ('IND'),
('UK'),
('FR')
) V(CC))
SELECT CASE WHEN UK > 0 AND FR > 0 AND C.CC = 'UK' THEN 'EU' ELSE C.CC END AS CC
FROM CTE C
WHERE (Ind > 0 AND UK > 0 AND FR > 0 AND C.CC IN ('UK','IND'))
OR (Ind = 0 AND UK > 0 AND FR > 0 AND C.CC = 'UK')
OR (Ind > 0 AND (UK = 0 OR FR = 0));
实际上,轮转数据可能更好。但是工作量很少,我不得不做一些假设(不是我要做的事情)。