table1
,如下所示
+-------------------------+--------+--------+--------+----------+---------+---------+
| time | regno | radar | speed | heading | people | lights |
+-------------------------+--------+--------+--------+----------+---------+---------+
| 2017/03/28 06:00:06.112 | 11 | 1100 | 50 | down | 4 | false |
| 2017/03/28 06:00:06.112 | 12 | 1111 | 40 | up | 1 | false |
| 2017/03/28 06:00:06.113 | 11 | 1100 | 50 | down | 4 | false |
| 2017/03/28 06:00:07.100 | 21 | 1100 | 60 | down | 4 | false |
+-------------------------+--------+--------+--------+----------+---------+---------+
我需要生成一个输出表,其中捕获来自table1
的选择字段的聚合值,如下所示。
+-----------+-------+------+------+---------+
| attribute | avg | min | max | median |
+-----------+-------+------+------+---------+
| speed | 50 | 40 | 60 | 40 |
| people | 3.25 | 1 | 4 | 4 |
+-----------+-------+------+------+---------+
我可以在临时表中单独聚合每个属性,然后对临时表进行联合以获得上面的输出,但是当有大量属性(在我的情况下大约是20)时它会变得难以处理 - 是否存在更聪明的方式来解决这个问题?
答案 0 :(得分:0)
你可以通过联合查询来完成这个,试试这个
SELECT avg(speed),min(speed),max(speed),'speed' as speed FROM `table1`
UNION
SELECT avg(people),min(people),max(people),'people' as people FROM `table1`