Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL sorting by number and stay grouped

Tags:

sql

sorting

mysql

Thank you for the time you'll spend to read and possibly answered to my question.

I've a little problem of sorting I show you :

My SQL query is that :

SELECT p.advisor_create, COUNT(p.id) AS Nb, IF(p.cancel_advisor IS 
NULL, "no", "yes") AS refund
FROM payment p
WHERE p.type = 'CESSATION'
AND p.date BETWEEN '2017-01-01 00:00:00' AND '2017-11-31 23:59:59'
GROUP BY p.advisor_create, refund
ORDER BY p.advisor_create, Nb DESC

The data I get from this is like that :

+-------+---+------+
|advisor| NB|refund|
+-------+---+------+
| 170432| 50|    no|
| 170432|  4|   yes|
| 175222| 30|    no|
| 175222|  3|   yes|
| 182985|304|    no|
| 182985| 19|   yes|
| 362912|360|    no|
| 362912| 13|   yes|
+-------+---+------+

And I would to sort like this :

+-------+---+------+
|advisor| NB|refund|
+-------+---+------+
| 362912|360|    no|
| 362912| 13|   yes|
| 182985|304|    no|
| 182985| 19|   yes|
| 170432| 50|    no|
| 170432|  4|   yes|
| 175222| 30|    no|
| 175222|  3|   yes|
+-------+---+------+

Sort by MAX(NB) of same "advisor" and group the two line of an advisor together.

Thank you again for your help.

The solution : Thank to @gordon-linoff for this

SELECT p.advisor_create, COUNT(p.id) AS Nb,
   IF(p.cancel_advisor IS NULL, 'no', 'yes') AS refund
FROM payment p
WHERE p.type = 'CESSATION' AND
  p.date >= '2017-01-01' AND 
  p.date < '2017-12-01'
GROUP BY p.advisor_create, refund
ORDER BY (SELECT COUNT(*)
      FROM payment p2
      WHERE p2.advisor_create = p.advisor_create AND
            p2.type = 'CESSATION' AND
            p2.date >= '2017-01-01' AND 
            p2.date < '2017-12-01' AND
            p2.cancel_advisor IS NULL
     ) DESC,
     p.advisor_create, Nb DESC
like image 758
David Allios Avatar asked Nov 29 '17 14:11

David Allios


People also ask

Can I use ORDER BY and GROUP BY together?

Both GROUP BY and ORDER BY are clauses (or statements) that serve similar functions; that is to sort query results. However, each of these serve very different purposes; so different in fact, that they can be employed separately or together.

Can you ORDER BY and GROUP BY SQL?

Order By and Group By Clause 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.

Can you GROUP BY count in SQL?

SQL – count() with Group By clause The count() function is an aggregate function use to find the count of the rows that satisfy the fixed conditions. The count() function with the GROUP BY clause is used to count the data which were grouped on a particular attribute of the table.

How do I group the same values in SQL?

The SQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.


1 Answers

You can use a subquery in the order by. This allows you to do:

SELECT p.advisor_create, COUNT(p.id) AS Nb,
       IF(p.cancel_advisor IS NULL, 'no', 'yes') AS refund
FROM payment p
WHERE p.type = 'CESSATION' AND
      p.date >= '2017-01-01' AND 
      p.date < '2017-12-01'
GROUP BY p.advisor_create, refund
ORDER BY (SELECT COUNT(*)
          FROM payment p2
          WHERE p2.advisor_create = p.advisor_create AND
                p2.type = 'CESSATION' AND
                p2.date >= '2017-01-01' AND 
                p2.date < '2017-12-01' AND
                p2.cancel_advisor IS NULL
         ),
         p.advisor_create, Nb DESC;

This assumes you want to sort by the "no" value. If you want to sort by the total, just remove that condition from the subquery.

Also notice that I simplified the date comparisons. There is no reason to go to seconds -- in fact, between is not recommended for date/time comparisons, because of the confusion that arises with the time component.

like image 131
Gordon Linoff Avatar answered Nov 15 '22 06:11

Gordon Linoff