0
Number | UserId | Priority
--------------------------
1234   | User1  | 1
2345   | User1  | 2
5678   | User1  | 3
2456   | User2  | 1
6556   | User2  | 2
2435   | User3  | 1
6567   | User3  | 2

我想根据优先级的最高值提取下面的行列表

Number | UserId | Priority
--------------------------
5678   | User1  | 3
6556   | User2  | 2
6567   | User3  | 2

将表名视为用户代码,有人可以为此提供 sql 查询帮助。

4

3 回答 3

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中工作。

于 2013-08-01T15:15:43.053 回答
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

Sql 小提琴演示

于 2013-08-01T15:05:41.807 回答
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
于 2013-08-01T15:20:10.170 回答