我有一个这样的表,并且有输入参数RevParam:
Key Rev IsCurrent
=====================
abc 0 TRUE
def 0 TRUE
ghj 2 FALSE
ghj 0 TRUE
klm 0 TRUE
def 1 FALSE
abc 1 FALSE
abc 2 FALSE
我希望Key
成为唯一值。如果设置了RevParam
,则应查看具有该Rev值的所有行,缺少的Key
应该具有:版本0.如下所示:
Result if RevParam = 1
abc 1 FALSE
def 1 FALSE
ghj 0 TRUE
klm 0 TRUE
Result if revisionParameter = 0
abc 0 TRUE
def 0 TRUE
ghj 0 TRUE
klm 0 TRUE
这一直困扰着我,请帮助我!
答案 0 :(得分:4)
WITH records
AS
(
SELECT [key], [rev], [IsCurrent],
ROW_NUMBER() OVER(PARTITION BY [key]
ORDER BY CASE WHEN [rev] = 1 -- <<== change REV value here
THEN 0 ELSE 1 END) rn
FROM tableName
WHERE [Rev] IN (1,0)
)
SELECT [key], [rev], [IsCurrent]
FROM records
WHERE rn = 1
答案 1 :(得分:0)
结果如果RevParam = 1
abc 1 FALSE def 1 FALSE ghj 0是的 klm 0 TRUE
它的转速值为0以及1.请明确指出