I have a multi column table that I have derived by joining other tables
UserId. | Attempts. | quizno |
---|---|---|
First | 1 | assd. |
First | 2 | ffjfjf. |
Second | 1 | cbcbc. |
Second | 2 | tititi. |
Second | 3 | cncncn. |
I want a table that returns the row for each user with the highest attempt
UserId. | Attempts. | quizno |
---|---|---|
First | 2 | ffjfjf |
Second | 3 | cncncn |
3
Answers
Based on your comment, I guess that your table has more than 2 columns rather than just two.
If you only have 2 columns, what @Abra put in the comment works.
One suggestion is:
That’s a
distinct on
. It selects the first/top per group: demoYou’re telling it in the parentheses which fields decide what a group is, similar to a
group by
, then in theorder by
you need to repeat that list followed by the fields which together decide which rows ends up as the "top" one.My solution.
Creating the [database] table:
I populate the table with sample data from your question.
My query:
Note that table
T2
contains the maximum attempts per user.My results:
Refer to this db<>fiddle