MySQL结合COUNT,MAX和SUM

时间:2017-12-22 12:07:04

标签: mysql sql group-by

在MySQL中,我希望有一个额外的列显示特定列的值的总和。但是,我想要求和的数字来自子查询,而不是存储在单独的表中,如下所示:

(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

这将输出如下内容:

ID     MAX_COUNT
ABC    1
DEF    2
GHI    3

现在,我想要一个额外的列显示MAX_COUNT的总和,就像这样(在所有行上重复):

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

实际目标实际上是显示总MAX_COUNT的百分比MAX_COUNT,即1 / 6,2 / 6和3/6。 我该怎么做呢?我已经尝试过CROSS JOIN,但它不起作用:

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

错误:未知表'b'

示例

示例表:

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);

现在我想为COL1和COL2的每个组合提供每小时表格中的行数:

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

哪个输出:

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

现在我想要,对于COL1和COL2的每个组合,COUNT_LINES的最大值,所以我在子查询中使用上面的查询:

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;

哪个输出:

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

现在在最后一步,我想在单独的列中使用MAX(COUNT_LINES)列的SUM,如下所示:

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

但那是我不知道该怎么做的部分。

3 个答案:

答案 0 :(得分:0)

将CTE用于此

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

可以重写(对于较旧的MySQL)

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

答案 1 :(得分:0)

试试这个:

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 :(得分:0)

这似乎给出了正确答案:

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)