Skip to content
Advertisement

Order grouped table by id user sql

I want to order a grouped statement using as reference the number choosen by an specific user.

TABLE

expected result (at the end what user 1425 choosed)

I want to put the last row with the number choosed by the user. i just cant figure that out

Advertisement

Answer

You can aggregate and use a conditional max for ordering, like so:

If any row for the given type has an id_user that matches the chosen value, the conditional max returns 1, wich puts it last in the resultset. The second ordering criteria break the ties for groups that do not fulfill the condition.

If you are running MySQL, you can simplify the order by clause a little:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement