Whenever I use the max function I somehow lose all the connection to my other values, so that the row that is printed later doesn’t correlate with the column that I ran max on anymore.
So my table is:
user col1 col2 col3
1 1 2 3
1 3 4 5
2 2 3 1
3 1 1 3
3 2 4 6
4 5 1 5
So if I run
select user, col1, col2, max(col3) as col3
from table
group by user
order by user;
I would get
user col1 col2 col3
1 1 2 5
2 2 3 1
3 1 1 6
4 5 1 5
So the max value of col3 is correct, but it doesn’t get the correct row of that value.
What I want is to get the max value of a column and return that row for each user. If there are multiple max values that it should return all users, even if it has same user id.
Other databases (e.g. MS SQL Server) doesn’t let you mix aggergated values with non-aggregated values, just because you would get the wrong result.
So, if you want non-aggregated values from the record where the maximum value was, join against the table again: