Using a self join:
如果存在多种因素,所有价值将归并到底价值。
SELECT a.value
FROM TABLE a
JOIN (SELECT MAX(t.subkey) AS max_subkey
FROM TABLE t
WHERE t.key = 1) b ON b.max_subkey = a.subkey
WHERE a.key = 1
Using RANK & CTE (SQL Server 2005+):
如果存在多种因素,所有价值将归并到底价值。
WITH summary AS (
SELECT t.*,
RANK() OVER(ORDER BY t.subkey DESC) AS rank
FROM TABLE t
WHERE t.key = 1)
SELECT s.value
FROM summary s
WHERE s.rank = 1
Using ROW_NUMBER & CTE (SQL Server 2005+):
这将交还一行,即使有一个以上具有相同关键价值......。
WITH summary AS (
SELECT t.*,
ROW_NUMBER() OVER(ORDER BY t.subkey DESC) AS rank
FROM TABLE t
WHERE t.key = 1)
SELECT s.value
FROM summary s
WHERE s.rank = 1
Using TOP:
这将交还一行,即使有一个以上具有相同关键价值......。
SELECT TOP 1
t.value
FROM TABLE t
WHERE t.key = 1
ORDER BY t.subkey DESC