MySQL combining COUNT, MAX and SUM

这一生的挚爱 提交于 2019-12-20 03:21:34

问题


In MySQL, I would like to have an extra column showing the sum of values of a particular column. However, the numbers I would like to sum come from a subquery and are not stored in a separate table, something like this:

(SELECT a.ID, MAX(a.COUNT_ID) AS MAX_COUNT FROM
    (SELECT ID, COUNT(*) AS COUNT_ID
    FROM my_table
    GROUP BY COL1, COL2) a
 GROUP BY COL1, COL2) b

And this would output something like:

ID     MAX_COUNT
ABC    1
DEF    2
GHI    3

And now, I want an extra column showing the sum of MAX_COUNT, like this (repeated over all rows):

ID     MAX_COUNT    SUM_MAX_COUNT
ABC    1            6
DEF    2            6
GHI    3            6

The actual goal is actually to show the percentage MAX_COUNT of the total MAX_COUNT, so 1/6, 2/6 and 3/6. How do I do this? I already tried doing a CROSS JOIN but it doesn't work:

SELECT * FROM
    ((SELECT a.ID, MAX(a.COUNT_ID) AS MAX_COUNT FROM
        (SELECT ID, COUNT(*) AS COUNT_ID
        FROM my_table
        GROUP BY COL1, COL2) a
     GROUP BY COL1, COL2) b
CROSS JOIN (SELECT SUM(b.MAX_COUNT)) AS c

Error: Unknown table 'b'


EXAMPLE

Example table:

CREATE TABLE TABLE1 (
COL1 varchar(255),
COL2 varchar(255),
DAY int,
HOUR int);

INSERT INTO TABLE1 VALUES
('X','Y',1,12),
('X','Y',1,13),
('X','Y',1,13),
('A','B',2,19),
('X','B',3,13),
('X','B',3,13);

Now I want to have, for each combination of COL1 and COL2, the number of lines in this table for each hour:

SELECT COL1, COL2, HOUR, COUNT(*) AS COUNT_LINES
FROM TABLE1
GROUP BY DAY, HOUR, COL1, COL2;

Which outputs this:

COL1    COL2    HOUR    COUNT_LINES
X       Y       12      1
X       Y       13      2
A       B       19      1
X       B       13      2

Now I want, for each combination of COL1 and COL2, the maximum of COUNT_LINES, so I use the query above in a subquery:

SELECT a.COL1, a.COL2, MAX(a.COUNT_LINES)
FROM 
    (SELECT COL1, COL2, HOUR, COUNT(*) AS COUNT_LINES
    FROM TABLE1
    GROUP BY DAY, HOUR, COL1, COL2) a
GROUP BY COL1, COL2;

Which outputs this:

COL1    COL2    MAX(COUNT_LINES)
A       B       1
X       B       2
X       Y       2

Now in the last step, I want the SUM of MAX(COUNT_LINES) column in a separate column, like this:

COL1    COL2    MAX(COUNT_LINES)    SUM(MAX(COUNT_LINES))
A       B       1                   5
X       B       2                   5
X       Y       2                   5

But that is the part I don't know how to do.


回答1:


Try this:

SELECT COL1,Max(COUNT_ID),Sum(SUM_MAX_COUNT) FROM(
SELECT COL1,
   Count(*) AS COUNT_ID,
   (SELECT Sum(Count(*))
    FROM   TABLE_NAME
    GROUP BY COL1) AS SUM_MAX_COUNT
FROM   TABLE_NAME
GROUP  BY COL1)
GROUP BY COL1;



回答2:


Use CTE for this

with q as
(
  SELECT a.COL1, a.COL2, MAX(a.COUNT_LINES) cmax
  FROM 
    (SELECT COL1, COL2, HOUR, COUNT(*) AS COUNT_LINES
    FROM TABLE1
    GROUP BY DAY, HOUR, COL1, COL2) a
  GROUP BY COL1, COL2
)
SELECT q.*, (SELECT SUM(q.cmax) from q)
FROM q

dbfiddle demo

which can be rewritten (for older MySQL) as

SELECT q.*, 
(
  SELECT SUM(cmax) from 
  (
    SELECT a.COL1, a.COL2, MAX(a.COUNT_LINES) cmax
    FROM 
    (SELECT COL1, COL2, HOUR, COUNT(*) AS COUNT_LINES
    FROM TABLE1
    GROUP BY DAY, HOUR, COL1, COL2) a
    GROUP BY COL1, COL2  
  ) q
) as max_sum
FROM 
(
  SELECT a.COL1, a.COL2, MAX(a.COUNT_LINES) cmax
  FROM 
    (SELECT COL1, COL2, HOUR, COUNT(*) AS COUNT_LINES
    FROM TABLE1
    GROUP BY DAY, HOUR, COL1, COL2) a
  GROUP BY COL1, COL2
) q

dbfiddle demo




回答3:


This seems to give the right answer:

mysql-sql> select
       ...     x.col1,
       ...     x.col2,
       ...     x.max_count_lines,
       ...     y.sum_max_count_lines
       ... from
       ...     (  
       ...         select
       ...             a.col1,
       ...             a.col2,
       ...             max(a.count_lines) as max_count_lines
       ...         from 
       ...             (
       ...                 select
       ...                     col1,
       ...                     col2,
       ...                     hour,
       ...                     count(*) as count_lines
       ...                 from
       ...                     table1
       ...                 group by
       ...                     day,
       ...                     hour,
       ...                     col1,
       ...                     col2
       ...              ) a
       ...         group by
       ...              col1,
       ...              col2
       ...     ) x,
       ...     (
       ...         select
       ...             sum(max_count_lines) as sum_max_count_lines
       ...         from
       ...             (  
       ...                 select
       ...                     b.col1,
       ...                     b.col2,
       ...                     max(b.count_lines) as max_count_lines
       ...                 from 
       ...                     (
       ...                         select
       ...                             col1,
       ...                             col2,
       ...                             hour,
       ...                             count(*) as count_lines
       ...                         from
       ...                             table1
       ...                         group by
       ...                             day,
       ...                             hour,
       ...                             col1,
       ...                             col2
       ...                      ) b
       ...                 group by
       ...                      col1,
       ...                      col2
       ...             ) c
       ...     ) y;
+------+------+-----------------+---------------------+
| col1 | col2 | max_count_lines | sum_max_count_lines |
+------+------+-----------------+---------------------+
| A    | B    |               1 |                   5 |
| X    | B    |               2 |                   5 |
| X    | Y    |               2 |                   5 |
+------+------+-----------------+---------------------+
3 rows in set (0.00 sec)


来源:https://stackoverflow.com/questions/47941262/mysql-combining-count-max-and-sum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!