N
N
Night_Harpy2020-07-19 15:37:42
MySQL
Night_Harpy, 2020-07-19 15:37:42

Why doesn't output sql in descending order?

select DATE_FORMAT( time_reports.date, '%W'), group_concat(employees.name, ' (', x.total_hours, ' hours)' SEPARATOR  ', '  LIMIT 3)  
  from employees
  inner join time_reports on employees.id=time_reports.employee_id
  inner join 
    (
      select employee_id, round(hours,2) as total_hours 
        from time_reports group by employee_id
         ORDER BY total_hours DESC
       )x on x.employee_id=time_reports.employee_id
  group by time_reports.date


x.total_hours should output 3 maximum values. Limit 3 works, but here's how to make ORDER BY total_hours DESC what would work (I put it in the second SELECT but the effect is zero), if you put it at the very end, then it works not only on total_hours but also on DATE_FORMAT, but you need only on total_hours

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2020-07-19
@Night_Harpy

select DATE_FORMAT( time_reports.date, '%W'), group_concat(employees.name, ' (', x.total_hours, ' hours)' order by x.total_hours desc SEPARATOR  ', '  LIMIT 3)

L
Lazy @BojackHorseman MySQL, 2020-07-19
Tag

because in the resulting selection no sorting order is explicitly specified, and there is no such field at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question