phpmysqlsql-order-byaggregate-functionscodeigniter-3

ORDER BY SUM() but provide non-aggregated result set with CodeIgniter SELECT query


I want to get the results of similar number rows together that means one after another and each row will be having different counts of data and the rows which have higher count should come first.

Here is my code

public function get_all_mostly_entered_numbers($limit, $offset)
{
    
    $query = $this->db->query("SELECT *, SUM(quantity) AS MOST_FREQUENT
         FROM datas
         WHERE date_status=1
         GROUP BY number,type
         ORDER BY SUM(quantity) DESC LIMIT 200");

    return $query->result();
}

the result is coming like this

type        number       count
sup          555         280
sup          000         245
sup          777         235
sup          888         235
bo           000         40
bo           777         30
bo           888         25
bo           555         10

I want my result to be like this

type        number       count
sup          555         280
bo           555         10
sup          000         245
bo           000         40
sup          777         235
bo           777         30
sup          888         235
bo           888         25

Solution

  • Looking to the sample provided

    You could use a join of the result with the max result and order based ( in th sample there a max_val colum for better understand the result)

     select T.number, max(T.MOST_FREQUENT) max_val, W.MOST_FREQUENT, W.Type
      from (
    
      SELECT *, SUM(quantity) AS MOST_FREQUENT
           FROM datas
           WHERE date_status=1
           GROUP BY number,type
           ORDER BY SUM(quantity) DESC LIMIT 200 ) T 
      INNER JOIN (
      SELECT *, SUM(quantity) AS MOST_FREQUENT
               FROM datas
               WHERE date_status=1
               GROUP BY number,type
               ORDER BY SUM(quantity) DESC LIMIT 200
      ) W ON T.number = W.number 
      group by T.number, , W.MOST_FREQUENT, W.Type
      ORDER BY  max(T.MOST_FREQUENT)  DESC
               , (max(T.MOST_FREQUENT)=W.MOST_FREQUENT) DESC
               ,  W.MOST_FREQUENT
               , W.Type
    

    You can not show max_val simply select from the result (ordered ) only the column you need

    And for avoid the wrong sequence with same max_row you could try

    select T.number, max(T.MOST_FREQUENT) max_val, W.MOST_FREQUENT, W.Type
      from (
    
      SELECT *, SUM(quantity) AS MOST_FREQUENT
           FROM datas
           WHERE date_status=1
           GROUP BY number,type
           ORDER BY SUM(quantity) DESC LIMIT 200 ) T 
      INNER JOIN (
      SELECT *, SUM(quantity) AS MOST_FREQUENT
               FROM datas
               WHERE date_status=1
               GROUP BY number,type
               ORDER BY SUM(quantity) DESC LIMIT 200
      ) W ON T.number = W.number 
      group by T.number, , W.MOST_FREQUENT, W.Type
      ORDER BY  concat(lpad(max(T.MOST_FREQUENT), 10, '0'), T.number)  DESC
               , (concat(max(T.MOST_FREQUENT, T.number))= concat(W.MOST_FREQUENT, T.number)) DESC
               ,  W.MOST_FREQUENT
               , W.Type