2013-04-26 44 views
1

我火了下面的查询到我的表:MySQL查询 - 分组值和Aggregrating

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)的唯一组合的计数,使得输出sh应该看起来像这样:

+-----------+---------------+----------+ 
| 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 | 
+-----------+---------------+----------+ 

有没有人可以给我一些建议。根据设定您所提供的结果

+0

这是有效的检查按platform_type列的值'AND platform_type NOT LIKE“total'' – 2013-04-26 02:39:17

回答

3

使用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; 
+0

我已经试过这一个,它不会给我每种平台类型的聚合结果......它确实做了计数......但没有给出所需的输出 – 2013-04-26 18:41:09

+0

这就是正是我所说的......应该坚持我的枪支。 – 2013-04-26 19:15:01

+0

@PiHorse我想我现在已经更好地理解了你的问题。请测试这个新的查询。 – 2013-04-26 19:17:17

0

,它看起来像你想也按category_name

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

我已经尝试过这一个,它并没有给我每个平台类型的汇总结果......它做计数......但没有给出所需的输出 – 2013-04-26 18:41:49

+0

@PiHorse究竟是什么问题?您能否请澄清 – 2013-04-26 18:51:55

+0

我在编辑中添加了建议查询的结果...您可以查看一下。 – 2013-04-26 19:02:54