简体   繁体   中英

How to get max value of a grouped counted variable in MySQL

I have a MySQL table like this;

recordID| netcall    | sign | activity | netid
1       | group1     | wa1  | 1        | 20
2       | group2     | wa2  | 2        | 30
3       | group1     | wa2  | 1        | 20
4       | group2     | wa3  | 2        | 30
5       | group1     | wa1  | 1        | 40
6       | group3     | wa4  | 3        | 50
7       | group3     | wa4  | 3        | 50
8       | group1     | wa2  | 1        | 40
9       | group1     | wa1  | 1        | 40
10      | group2     | wa4  | 2        | 60

What I need from that is:

Netcall | count | activity | netid
Group1  | 3     | 1        | 40
Group2  | 2     | 2        | 30
Group3  | 2     | 3        | 50

I thought I could;

SELECT MAX(xx.mycount) AS MAXcount
  FROM (SELECT COUNT(tt.sign) AS mycount ,tt.activity
        FROM NetLog tt
       WHERE ID <> 0
       GROUP BY netcall) xx

But this only brings up the grand total not broken down by netcall. I don't see an example of this question but I'm sure there is one, I'm just asking it wrong.

Your example and desire output are too basic, you should try to expand so include more cases.

Right now you can get the desire output with:

SELECT `netcall`, COUNT(*) as `total`, MAX(`activity`) as `activity`
FROM t
GROUP BY `netcall`;

My guess is you can have different activities for group so you need multiples steps

  • Calculate the COUNT() for GROUP BY netcall, activity I call it q
  • Then see what is the MAX(total) for each netcall I call it p
  • Now you reuse q as o you have all the count, so just select the one with the max count.

SQL DEMO

SELECT o.`netcall`, o.total, o.`activity`
FROM (
      SELECT `netcall`, COUNT(*) `total`, `activity`
      FROM t
      GROUP BY `netcall`, `activity`
     ) o 
JOIN (     
      SELECT `netcall`, MAX(`total`) as `total`
      FROM (
        SELECT `netcall`, COUNT(*) `total`
        FROM t
        GROUP BY `netcall`, `activity`
      ) q  
      GROUP BY `netcall`
     ) p
  ON o.`netcall` = p.`netcall`
 AND o.`total` = p.`total`

With MySQL v8+ you can use cte and window function to simplify a little bit

with group_count as (
  SELECT `netcall`, COUNT(*) as total, `activity`        
  FROM t
  GROUP BY `netcall`, `activity`
), group_sort as (
  SELECT `netcall`, total, `activity`, 
          RANK() OVER (PARTITION BY `netcall`, `activity` ORDER BY total DESC) as rnk
  FROM group_count
)  
SELECT *
FROM group_sort
WHERE rnk = 1

This question is asked (and answered) every day on SO; it even has its own chapter in the MySQL manual, but anyway...

SELECT a.netcall
     , b.total
     , a.activity
  FROM netlog a
  JOIN
     ( SELECT netcall 
            , MAX(record_id) record_id
            , COUNT(*) total
         FROM netlog 
        GROUP 
           BY netcall 
    ) b
   ON b.netcall = a.netcall
  AND b.record_id = a.record_id
    SELECT k.netcall, k.netID,  MAX(k.logins) highest, 
AVG(k.logins) average, netDate, activity
                          FROM
                            (SELECT netID, netcall, COUNT(*) logins, DATE(`logdate`) as netDate, activity
                               FROM NetLog
                              WHERE netID <> 0 AND status = 1
                                AND netcall <> '0' AND netcall <> ''
                              GROUP BY netcall, netID) k
                            GROUP BY netcall  
                            ORDER BY highest DESC 

Resulted in:

Net Call    Highest Average Net ID  Sub Net Of...   ICS
214   309   Map Date    Activity
MESN    65  41.5294 339     214     309 MAP 2017-09-03  MESN
W0KCN   34  14.9597 1       214     309 MAP 2016-03-15  KCNARES Weekly 2m Voice Net
W0ERH   31  31.0000 883     214     309 MAP 2018-10-12  Johnson Co. Radio Amateurs Club Meeting Net
KCABARC 29  22.3333 57      214     309 MAP 2016-10-10  KCA Blind Amateurs Weekly 2m Voice Net

....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM