MySQL 查询 - 分组值和聚合

标签 mysql sql

我向我的表发出以下查询:

SELECT code_ver, 
       platform_type, 
       category_name 
FROM   builds 
WHERE  runtype LIKE 'test' 
       AND platform_type NOT LIKE 'total'; 

+-----------+---------------+---------------+
| code_ver  | platform_type | category_name |
+-----------+---------------+---------------+
| 10.0.1.50 | 8k            | UMTS          | 
| 10.0.1.50 | 8k            | UMTS          | 
| 10.0.1.50 | 8k            | IP            | 
| 10.0.1.50 | 8k            | IP            | 
| 10.0.1.50 | 9k            | IP            | 
| 10.0.1.50 | 9k            | IP            | 
| 10.0.1.50 | 9k            | UMTS          | 
| 10.0.1.50 | 9k            | UMTS          | 
| 10.0.1.50 | 9k            | UMTS          | 
| 10.0.1.50 | 9k            | Stability     | 
| 10.0.1.50 | 9k            | Stability     | 
| 10.0.1.51 | 8k            | UMTS          | 
| 10.0.1.51 | 8k            | UMTS          | 
| 10.0.1.51 | 8k            | IP            | 
| 10.0.1.51 | 8k            | IP            | 
| 11.0.1.50 | 9k            | UMTS          | 
| 11.0.1.50 | 9k            | IP            | 
+-----------+---------------+---------------+

我触发以下查询

SELECT code_ver, 
       platform_type, 
       Count(*) 
FROM   builds 
WHERE  runtype LIKE 'test' 
       AND platform_type NOT LIKE 'total' 
GROUP  BY code_ver, 
          platform_type; 

+-----------+---------------+----------+
| code_ver  | platform_type | count(*) |
+-----------+---------------+----------+
| 10.0.1.50 | 8k            |        4 | 
| 10.0.1.50 | 9k            |        7 | 
| 10.0.1.51 | 8k            |        4 | 
| 11.0.1.50 | 9k            |        2 | 
+-----------+---------------+----------+

当我触发以下查询时

SELECT code_ver, 
       platform_type, 
       Count(*) 
FROM   builds 
WHERE  runtype LIKE 'test' 
       AND platform_type NOT LIKE 'total' 
GROUP  BY code_ver, 
          platform_type,
          category_name; 

+-----------+---------------+----------+
| code_ver  | platform_type | count(*) |
+-----------+---------------+----------+
| 10.0.1.50 | 8k            |        2 | 
| 10.0.1.50 | 8k            |        2 | 
| 10.0.1.50 | 9k            |        2 | 
| 10.0.1.50 | 9k            |        2 | 
| 10.0.1.50 | 9k            |        3 | 
| 10.0.1.51 | 8k            |        2 | 
| 10.0.1.51 | 8k            |        2 | 
| 11.0.1.50 | 9k            |        1 | 
| 11.0.1.50 | 9k            |        1 | 
+-----------+---------------+----------+

但我想要 (code_ver + platform_type + category_name) 的唯一组合的计数,这样输出应该如下所示:

+-----------+---------------+----------+
| code_ver  | platform_type | count(*) |
+-----------+---------------+----------+
| 10.0.1.50 | 8k            |        2 | 
| 10.0.1.50 | 9k            |        3 | 
| 10.0.1.51 | 8k            |        2 | 
| 11.0.1.50 | 9k            |        2 | 
+-----------+---------------+----------+

谁能给我一些建议

最佳答案

使用 SQL 内部查询。

SELECT ta.code_ver, 
   ta.platform_type, 
   Count(*) 
FROM (SELECT code_ver, platform_type
      FROM   builds 
      WHERE  runtype LIKE 'test' 
          AND platform_type NOT LIKE 'total'
      GROUP  BY code_ver, platform_type, category_name) AS ta -- table alias
GROUP BY ta.code_ver, ta.platform_type;

关于MySQL 查询 - 分组值和聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16227834/

相关文章:

sql - DENSE_RANK 根据特定顺序

sql - MS Access SQL, "function calls"

mysql - 使用 presto 如何显示像 XXX 这样的表格

计算值的MySQL连接表

sql - 如何删除 MySQL 数据库中的所有表?

mysql - 将内容管理系统网站上传到互联网

mysql - 为什么mysql在级联表上构建时给我语法错误

c# - 为什么会出现 SQL 异常?

mysql - 在 MySQL 数据库中我想要 IF else 条件

MySQL查询从表中提取两条信息