select t.*, (case when c1 = @c1 then 1 else 0 end) + (case when c1 = @c2 then 1 else 0 end) + ... (case when c1 = @c5 then 1 else 0 end) + (case when c2 = @c1 then 1 else 0 end) + (case when c2 = @c2 then 1 else 0 end) + ... (case when c2 = @c5 then 1 else 0 end) + ... cnt from tb t order by cnt desc
------解决方案--------------------
SQL code
DECLARE @A INT,@B INT,@C INT,@D INT,@E INT
SELECT TOP 1 WITH TIES A,B,C,D,E
FROM TB
WHERE A=@A OR B=@B OR C=@C OR D=@D OR E=@E
ORDER BY
CASE WHEN A=@A THEN 1 ELSE 0 END
+CASE WHEN B=@B THEN 1 ELSE 0 END
+CASE WHEN C=@C THEN 1 ELSE 0 END
+CASE WHEN D=@D THEN 1 ELSE 0 END
+CASE WHEN E=@E THEN 1 ELSE 0 END
DESC
------解决方案-------------------- 要是自由组合的话,写个自定义函数能从小到大取数,然后全套上这个函数就行了。
------解决方案--------------------
------解决方案-------------------- 大乌龟小F看看这样行不
SQL code
DECLARE @A INT,@B INT,@C INT,@D INT,@E INT
SELECT TOP 1 WITH TIES A,B,C,D,E
FROM TB
WHERE A IN(@A,@B,@C,@D,@E)
OR B IN(@A,@B,@C,@D,@E)
OR C IN(@A,@B,@C,@D,@E)
OR D IN(@A,@B,@C,@D,@E)
OR E IN(@A,@B,@C,@D,@E)
ORDER BY
CASE WHEN A IN(@A,@B,@C,@D,@E) THEN 1 ELSE 0 END
+CASE WHEN B IN(@A,@B,@C,@D,@E) THEN 1 ELSE 0 END
+CASE WHEN C IN(@A,@B,@C,@D,@E) THEN 1 ELSE 0 END
+CASE WHEN D IN(@A,@B,@C,@D,@E) THEN 1 ELSE 0 END
+CASE WHEN E IN(@A,@B,@C,@D,@E) THEN 1 ELSE 0 END
DESC
------解决方案--------------------