MySQL UNION ALL如何运行agregate函数?

时间:2017-02-08 15:10:07

标签: mysql aggregate-functions union-all

我有一些SQL代码正是我想要的:

select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_10
union all
select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_11
union all
select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
from checklist_12

group by callsign, number
order by max_ts

结果:

number,name,max(id),max_ts,callsign,max(time_hint_utc),count(*)
10,Checklist 10 Foo,2,1486554484635,VRTEST,2017-02-08 12:21:32,2
11,Checklist 11 Bar,2,1486554490674,VRTEST,2017-02-08 12:21:39,2
12,Checklist 12 Baz,2,1486554496378,VRTEST,2017-02-08 12:21:44,2
12,Checklist 12 Baz,3,1486554496379,VRTEST2,2017-02-08 12:21:45,1

特别是,我看到max()count()的正确结果,即每个max / count的{​​{1}}和callsign组合,而不是个人number

这怎么办?聚合函数看起来像,就像它们在表级selects中一样,但它们的功能就像它们是selects'ed结果的函数一样。

P.S。很抱歉提出一个问题,唯一的问题是我缺乏理解。

使用表格描述进行更新:

union all

数据:

mysql> describe checklist_10;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field         | Type                                                         | Null | Key | Default | Extra          |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id            | bigint(20) unsigned                                          | NO   | PRI | NULL    | auto_increment |
| ts            | bigint(20) unsigned                                          | NO   |     | NULL    |                |
| callsign      | varchar(20)                                                  | NO   |     | NULL    |                |
| smg_id        | tinyint(3) unsigned                                          | NO   |     | NULL    |                |
| time          | int(11)                                                      | NO   |     | NULL    |                |
| time_hint_utc | datetime                                                     | NO   |     | NULL    |                |
| reason        | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO   |     | NULL    |                |
| foo0          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo1          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo2          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo3          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo4          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo5          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo6          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo7          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo8          | tinyint(1)                                                   | NO   |     | NULL    |                |
| foo9          | tinyint(1)                                                   | NO   |     | NULL    |                |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
17 rows in set (0.00 sec)

mysql> describe checklist_11;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field         | Type                                                         | Null | Key | Default | Extra          |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id            | bigint(20) unsigned                                          | NO   | PRI | NULL    | auto_increment |
| ts            | bigint(20) unsigned                                          | NO   |     | NULL    |                |
| callsign      | varchar(20)                                                  | NO   |     | NULL    |                |
| smg_id        | tinyint(3) unsigned                                          | NO   |     | NULL    |                |
| time          | int(11)                                                      | NO   |     | NULL    |                |
| time_hint_utc | datetime                                                     | NO   |     | NULL    |                |
| reason        | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO   |     | NULL    |                |
| bar0          | tinyint(1)                                                   | NO   |     | NULL    |                |
| bar1          | tinyint(1)                                                   | NO   |     | NULL    |                |
| bar2          | tinyint(1)                                                   | NO   |     | NULL    |                |
| bar3          | tinyint(1)                                                   | NO   |     | NULL    |                |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

mysql> describe checklist_12;
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| Field         | Type                                                         | Null | Key | Default | Extra          |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
| id            | bigint(20) unsigned                                          | NO   | PRI | NULL    | auto_increment |
| ts            | bigint(20) unsigned                                          | NO   |     | NULL    |                |
| callsign      | varchar(20)                                                  | NO   |     | NULL    |                |
| smg_id        | tinyint(3) unsigned                                          | NO   |     | NULL    |                |
| time          | int(11)                                                      | NO   |     | NULL    |                |
| time_hint_utc | datetime                                                     | NO   |     | NULL    |                |
| reason        | enum('UNKNOWN','PERIODIC','SHIFT','MENU','EVENT','DECLINED') | NO   |     | NULL    |                |
| baz0          | tinyint(1)                                                   | NO   |     | NULL    |                |
| baz1          | tinyint(1)                                                   | NO   |     | NULL    |                |
| baz2          | tinyint(1)                                                   | NO   |     | NULL    |                |
| baz3          | tinyint(1)                                                   | NO   |     | NULL    |                |
| baz4          | tinyint(1)                                                   | NO   |     | NULL    |                |
+---------------+--------------------------------------------------------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)

没有1054错误:

mysql> select * from checklist_10;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
| id | ts            | callsign | smg_id | time       | time_hint_utc       | reason   | foo0 | foo1 | foo2 | foo3 | foo4 | foo5 | foo6 | foo7 | foo8 | foo9 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
|  1 | 1486554385343 | VRTEST   |      7 | 1486556393 | 2017-02-08 12:19:53 | PERIODIC |    1 |    1 |    0 |    0 |    1 |    1 |    0 |    1 |    0 |    0 |
|  2 | 1486554484635 | VRTEST   |      7 | 1486556492 | 2017-02-08 12:21:32 | SHIFT    |    0 |    0 |    0 |    0 |    0 |    0 |    0 |    1 |    0 |    0 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+------+------+------+------+------+
2 rows in set (0.00 sec)

mysql> select * from checklist_11;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
| id | ts            | callsign | smg_id | time       | time_hint_utc       | reason   | bar0 | bar1 | bar2 | bar3 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
|  1 | 1486554457077 | VRTEST   |      7 | 1486556465 | 2017-02-08 12:21:05 | PERIODIC |    0 |    0 |    0 |    0 |
|  2 | 1486554490674 | VRTEST   |      7 | 1486556499 | 2017-02-08 12:21:39 | SHIFT    |    1 |    1 |    1 |    1 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+
2 rows in set (0.00 sec)

mysql> select * from checklist_12;
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
| id | ts            | callsign | smg_id | time       | time_hint_utc       | reason   | baz0 | baz1 | baz2 | baz3 | baz4 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
|  1 | 1486554476903 | VRTEST   |      7 | 1486556485 | 2017-02-08 12:21:25 | PERIODIC |    1 |    1 |    1 |    1 |    1 |
|  2 | 1486554496378 | VRTEST   |      7 | 1486556504 | 2017-02-08 12:21:44 | SHIFT    |    1 |    1 |    1 |    1 |    1 |
|  3 | 1486554496379 | VRTEST2  |      7 | 1486556505 | 2017-02-08 12:21:45 | SHIFT    |    1 |    1 |    1 |    1 |    1 |
+----+---------------+----------+--------+------------+---------------------+----------+------+------+------+------+------+
3 rows in set (0.00 sec)

2 个答案:

答案 0 :(得分:2)

它不像您认为的那样有效。根据{{​​3}},

  

如果在不包含GROUP BY子句的语句中使用组函数,则它等同于对所有行进行分组。

所以这里发生的是你有两个隐式group by(总是只返回一行,无论你的数据中是否有不同的呼号)和一个显式group by实际只有分组你的最后一次选择。

因此,对于MySQL,您的查询看起来像

(select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, 
    callsign, max(time_hint_utc), count(*)
from checklist_10)

union all

(select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, 
   callsign, max(time_hint_utc), count(*)
from checklist_11)

union all

(select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, 
   callsign, max(time_hint_utc), count(*)
from checklist_12
group by callsign, number)

order by max_ts

你可以测试这个,例如更改callsignchecklist_10中某一行的checklist_11。它不会按预期分组。或者你可以,例如在第一个或最后一个查询中将别名number重命名为number1。它将(仅)在第一种情况下工作,在后一种情况下,group by现在找不到列number

此外,在您的情况下,这仅在您禁用documentation sql模式时才有效,因为您的选择列表包含聚合函数以外的列。

答案 1 :(得分:0)

似乎group by与个人selects绑定,但order by对所选内容的union all进行操作。

解决方案是在每个选择中添加group by子句:

mysql> select 10 as number, "Checklist 10 Foo" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
    -> from checklist_10
    -> group by callsign
    -> 
    -> union all
    -> 
    -> select 11 as number, "Checklist 11 Bar" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
    -> from checklist_11
    -> group by callsign
    -> 
    -> union all
    -> 
    -> select 12 as number, "Checklist 12 Baz" as name, max(id), max(ts) as max_ts, callsign, max(time_hint_utc), count(*)
    -> from checklist_12
    -> group by callsign
    -> 
    -> order by max_ts
    -> ;
+--------+------------------+---------+---------------+----------+---------------------+----------+
| number | name             | max(id) | max_ts        | callsign | max(time_hint_utc)  | count(*) |
+--------+------------------+---------+---------------+----------+---------------------+----------+
|     10 | Checklist 10 Foo |       2 | 1486554484635 | VRTEST   | 2017-02-08 12:21:32 |        2 |
|     11 | Checklist 11 Bar |       2 | 1486554490674 | VRTEST   | 2017-02-08 12:21:39 |        2 |
|     12 | Checklist 12 Baz |       2 | 1486554496378 | VRTEST   | 2017-02-08 12:21:44 |        2 |
|     12 | Checklist 12 Baz |       3 | 1486554496379 | VRTEST2  | 2017-02-08 12:21:45 |        1 |
|     10 | Checklist 10 Foo |       3 | 1486554496380 | VRTEST2  | 2017-02-08 12:21:46 |        1 |
+--------+------------------+---------+---------------+----------+---------------------+----------+
5 rows in set (0.00 sec)