Number | UserId | Priority
--------------------------
1234 | User1 | 1
2345 | User1 | 2
5678 | User1 | 3
2456 | User2 | 1
6556 | User2 | 2
2435 | User3 | 1
6567 | User3 | 2
我想根据Priority
的最高值提取下面的行列表Number | UserId | Priority
--------------------------
5678 | User1 | 3
6556 | User2 | 2
6567 | User3 | 2
将表名称视为用户代码,可以为此人提供sql查询帮助。
答案 0 :(得分:1)
Select A.number,A.userId,A.Priority from TableName A
inner join
(
Select UserId,Max(Priority) as Priority from TableName group by UserId
) B on A.Priority =B.Priority and A.UserId=B.UserId
答案 1 :(得分:1)
另一种写作方式是
select
a.*
from
TableName a
left join TableName b
on a.Priority < b.Priority
and a.UserId = b.UserId
where b.Priority is null
LEFT JOIN的工作原理是,当a.priority处于最大值时,没有b.priority具有更大的值,b行值将为NULL。
看到它在sqlfiddle中正常工作。
答案 2 :(得分:0)
SELECT A.Number, A.UserId, A.Priority
FROM usercodeTBL A
INNER JOIN(SELECT UserId, Max(Priority) AS Priority FROM usercodeTBL GROUP BY UserId) B
ON A.Priority = B.Priority AND A.UserId = B.UserId
ORDER BY UserId, Priority DESC