Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group by DESC order

I have the following table called questions:

ID | asker  1  | Bob 2  | Bob 3  | Marley 

I want to select each asker only once and if there are multiple askers with the same name, select the one of the highest id. So, the expected results:

ID | asker  3  | Marley 2  | Bob 

I use the following query:

SELECT * FROM questions GROUP by questions.asker ORDER by questions.id DESC 

I get the following result:

ID | asker  3  | Marley 1  | Bob 

It selects the first 'Bob' it encounters instead of the last one.

like image 625
Michael Samuel Avatar asked Mar 13 '13 15:03

Michael Samuel


People also ask

Can you use DESC with GROUP BY?

Notice the DESC in the GROUP BY clause sorts the status in descending order. And you can also use the ASC explicitly in the GROUP BY clause to sort the groups by status in ascending order.

Can we use DESC with GROUP BY in SQL?

Order By and Group By Clause in SQL In this SQL tutorial, we will learn how to use Order by and Group By in SQL. Group By in SQL is used to arrange similar data into groups and Order By in SQL is used to sort the data in ascending or descending order.

How do I sort my DESC order?

The SQL ORDER BY Keyword The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

How do you ORDER BY GROUP BY together?

Using Group By and Order By Together When combining the Group By and Order By clauses, it is important to bear in mind that, in terms of placement within a SELECT statement: The GROUP BY clause is placed after the WHERE clause. The GROUP BY clause is placed before the ORDER BY clause.


1 Answers

If you want the last id for each asker, then you should use an aggregate function:

SELECT max(id) as id,     asker FROM questions  GROUP by asker  ORDER by id DESC 

The reason why you were getting the unusual result is because MySQL uses an extension to GROUP BY which allows items in a select list to be nonaggregated and not included in the GROUP BY clause. This however can lead to unexpected results because MySQL can choose the values that are returned. (See MySQL Extensions to GROUP BY)

From the MySQL Docs:

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. ... You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values the server chooses.

Now if you had other columns that you need to return from the table, but don't want to add them to the GROUP BY due to the inconsistent results that you could get, then you could use a subquery to do so. (Demo)

select    q.Id,   q.asker,   q.other -- add other columns here from questions q inner join (   -- get your values from the group by   SELECT max(id) as id,      asker   FROM questions    GROUP by asker  ) m   on q.id = m.id order by q.id desc 
like image 139
Taryn Avatar answered Oct 03 '22 02:10

Taryn