patternsqlModerate
Query to select max value on join
Viewed 0 times
queryjoinvaluemaxselect
Problem
I have a table of Users:
and Levels
And I'm looking for a query to get the level for each user. Something along the lines of:
Such that the results would be:
Does anyone have any ideas or suggestions on how could I do this without resorting to cursors?
|Username|UserType|Points|
|John |A |250 |
|Mary |A |150 |
|Anna |B |600 |and Levels
|UserType|MinPoints|Level |
|A |100 |Bronze |
|A |200 |Silver |
|A |300 |Gold |
|B |500 |Bronze |And I'm looking for a query to get the level for each user. Something along the lines of:
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.UsernameSuch that the results would be:
|Username|UserType|Points|Level |
|John |A |250 |Silver |
|Mary |A |150 |Bronze |
|Anna |B |600 |Bronze |Does anyone have any ideas or suggestions on how could I do this without resorting to cursors?
Solution
Your existing query is close to something that you could use but you can get the result easily by making a few changes. By altering your query to use the
Here's a SQL Fiddle with a demo. This produces a result:
APPLY operator and implementing CROSS APPLY. This will return the row that meets your requirements. Here's a version that you could use:SELECT
u.Username,
u.UserType,
u.Points,
lv.Level
FROM Users u
CROSS APPLY
(
SELECT TOP 1 Level
FROM Levels l
WHERE u.UserType = l.UserType
and l.MinPoints < u.Points
ORDER BY l.MinPoints desc
) lv;Here's a SQL Fiddle with a demo. This produces a result:
| Username | UserType | Points | Level |
|----------|----------|--------|--------|
| John | A | 250 | Silver |
| Mary | A | 150 | Bronze |
| Anna | B | 600 | Bronze |Code Snippets
SELECT
u.Username,
u.UserType,
u.Points,
lv.Level
FROM Users u
CROSS APPLY
(
SELECT TOP 1 Level
FROM Levels l
WHERE u.UserType = l.UserType
and l.MinPoints < u.Points
ORDER BY l.MinPoints desc
) lv;| Username | UserType | Points | Level |
|----------|----------|--------|--------|
| John | A | 250 | Silver |
| Mary | A | 150 | Bronze |
| Anna | B | 600 | Bronze |Context
StackExchange Database Administrators Q#129340, answer score: 18
Revisions (0)
No revisions yet.