First order by importance then group by section In this table scheme http://sqlfiddle.com/#!9/101a9c/9/0

id  section     rev     importance
2   1           b       6
1   1           a       5
3   2           c       9
5   3           e       6
4   3           d       5


i want to order by **importance** then group by **section**

so i tried to use GROUP BY docs.section 

SELECT docs.id, docs.section, docs.rev, (
  SELECT SUM(docs_importance.importance) 
  FROM docs_importance
  WHERE docs_importance.doc_id = docs.id
) AS importance
FROM docs
GROUP BY docs.section    ---GROUPING
ORDER BY docs.section, importance DESC


but got this result:

id  section     rev     importance
1   1           a       5
3   2           c       9
4   3           d       5


but the result i want is supposed to be

id  section     rev     importance
2   1           b       6
3   2           c       9
5   3           e       6
i used this query

SELECT t.id, t.section, t.rev, t.importance FROM (
   SELECT docs.id, docs.section, docs.rev, (SELECT SUM(docs_importance.importance) FROM docs_importance WHERE docs_importance.doc_id = docs.id) AS importance
   FROM docs
   GROUP BY docs.id
   ORDER BY importance DESC
) AS t
GROUP BY t.section


where i first made a subquery as a temporary table then ordered it by importance then grouped the outer query by section to get the final result.