我有下表:
SELECT * FROM trips_motion_xtics
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| session_id | trip_id | lat_start | lat_end | lon_start | lon_end | alt | distance | segments_length | speed | acceleration | track | travel_mode |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 74.208 | 0 | foot |
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 74.154 | 0 | foot |
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 68.226 | 0 | foot |
| 656 | 303637 | 41.14454009 | 41.1631127 | -8.56292593 | -8.5870161 | 0 | 5921.07030809987 | 2785.6088546142 | 0 | 99.028 | 0 | car |
| 656 | 303637 | 41.14454009 | 41.1631127 | -8.56292593 | -8.5870161 | 0 | 5921.07030809987 | 2785.6088546142 | 0 | 109.992 | 0 | car |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
现在想计算alt, distance, speed ...
的唯一值的列session_id, trip_id, lat_start,..
的平均值。
查询:
SELECT DISTINCT(session_id, trip_id, lat_start, lat_end, lon_start, lon_end, travel_mode), AVG(alt) AS avg_alt, AVG(distance) AS avg_disntance, AVG(speed) AS avg_speed, AVG(acceleration) AS avg_acc FROM akil.trips_motion_xtics;
ERROR: column "trips_motion_xtics.session_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT DISTINCT(session_id, trip_id, lat_start, lat_end, lon...
必填结果:
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| session_id | trip_id | lat_start | lat_end | lon_start | lon_end | alt | distance | segments_length | speed | acceleration | track | travel_mode |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
| 652 | 303633 | 41.1523521 | 41.1524966 | -8.6097233 | -8.6096833 | 0 | 42.7424443438547 | 28.0353622436523 | 0 | 72.196 | 0 | foot |
| 656 | 303637 | 41.14454009 | 41.1631127 | -8.56292593 | -8.5870161 | 0 | 5921.07030809987 | 2785.6088546142 | 0 | 104.51 | 0 | car |
+------------+---------+-------------+------------+-------------+------------+-----+------------------+------------------+-------+--------------+-------+-------------+
答案 0 :(得分:1)
您要聚合。对于GROUP BY
子句中列出的列的每种组合,您将获得唯一的记录,并且可以将聚合函数(例如AVG()
)应用于其他列:
SELECT
session_id,
trip_id,
lat_start,
lat_end,
lon_start,
lon_end,
travel_mode,
AVG(alt) AS avg_alt,
AVG(distance) AS avg_disntance,
AVG(speed) AS avg_speed,
AVG(acceleration) AS avg_acc
FROM akil.trips_motion_xtics
GROUP BY
session_id,
trip_id,
lat_start,
lat_end,
lon_start,
lon_end,
travel_mode