我有一个用户表:
|Username|UserType|Points|
|John |A |250 |
|Mary |A |150 |
|Anna |B |600 |
和级别
|UserType|MinPoints|Level |
|A |100 |Bronze |
|A |200 |Silver |
|A |300 |Gold |
|B |500 |Bronze |
我正在寻找一个查询来获取每个用户的级别。类似于以下内容:
SELECT *
FROM Users U
INNER JOIN (
SELECT TOP 1 Level, U.UserName
FROM Levels L
WHERE L.MinPoints < U.Points
ORDER BY MinPoints DESC
) UL ON U.Username = UL.Username
这样的结果将是:
|Username|UserType|Points|Level |
|John |A |250 |Silver |
|Mary |A |150 |Bronze |
|Anna |B |600 |Bronze |
是否有人对我不使用游标该怎么做有任何想法或建议?