我有一个具有不同语言记录的场景,我需要的是获取单个行中的所有数据,如果某些数据不是必需的语言,则下一个默认语言进入该记录的操作,依此类推。
declare @LanguageId int = 3
select * from Purpose where LanguageId=(CASE WHEN @LanguageId = 1 THEN CASE WHEN EXISTS
(SELECT PurposeId
FROM Purpose
WHERE LanguageId = @LanguageId) THEN @LanguageId WHEN EXISTS
(SELECT PurposeId
FROM Purpose
WHERE LanguageId = 2) THEN 2 ELSE 3 END WHEN @LanguageId = 2 THEN CASE WHEN EXISTS
(SELECT PurposeId
FROM Purpose
WHERE LanguageId = @LanguageId) THEN @LanguageId WHEN EXISTS
(SELECT PurposeId
FROM Purpose
WHERE LanguageId = 1) THEN 1 ELSE 3 END WHEN @LanguageId = 3 THEN CASE WHEN EXISTS
(SELECT PurposeId
FROM Purpose
WHERE LanguageId = @LanguageId) THEN @LanguageId WHEN EXISTS
(SELECT PurposeId
FROM Purpose
WHERE LanguageId = 2) THEN 2 ELSE 1 END END)
我已经进行了查询并且工作正常,我正在寻找替代的替代优化查询。欢迎任何帮助或建议。
答案 0 :(得分:0)
这可能对您有用:
SELECT
p1.PurposeId, MIN( ISNULL( p2.LanguageId, p1.LanguageId ) ) AS LanguageId
FROM Purpose p1
LEFT OUTER JOIN Purpose p2 on p1.PurposeId = p2.PurposeId AND p2.LanguageId = :desiredLanguage
GROUP BY p1.PurposeId
如果有条目,则产生desiredLanguage
,或者每个PurposeId产生最低的可用LanguageId。