简化SQL查询
我正在使用 https://www.bixi.com/en/开放数据并被要求找到“数据集中每年一年组合的平均旅行数量”。 表
一个 | 是 | 的 | : | 我 | 示例 | 这 |
---|---|---|---|---|---|---|
的 | 要 | 查询 | | | | |
| 15 17:16:00 | 6315 | 2016-04-15 17:37:00 | 6315 | 1293 | 0 |
我过去成功回答问题的查询如下,但是我该如何简化它?
SELECT
daily_trips_2016.avg_daily_trips AS avg_daily_trips_2016,
daily_trips_2017.avg_daily_trips AS avg_daily_trips_2017,
daily_trips_2016.month
FROM
(
SELECT -- This sub-query determines the average number of daily trips per month for the year 2016
ROUND(COUNT(*) / (COUNT(DISTINCT DAY(start_date))), 0) AS avg_daily_trips,
MONTH(start_date) AS month
FROM trips
WHERE YEAR(start_date) = 2016
GROUP BY month) AS daily_trips_2016
LEFT JOIN
(
SELECT -- This sub-query determines the average number of daily trips per month for the year 2017
ROUND(COUNT(*) / (COUNT(DISTINCT DAY(start_date))), 0) AS avg_daily_trips,
MONTH(start_date) AS month
FROM trips
WHERE YEAR(start_date) = 2017
GROUP BY month) AS daily_trips_2017
ON daily_trips_2016.month = daily_trips_2017.month; -- Since both sub-queries share the "month" column, the JOIN happens on this column
-- and we can see the daily averages side by side for each year
I'm using the Bixi public dataset found at https://www.bixi.com/en/open-data and have been asked to find "the average number of trips a day for each year-month combination in the dataset". Here's an example of the table from which I'm querying:
id | start_date | start_station_code | end_date | end_station_code | duration_sec | is_member |
---|---|---|---|---|---|---|
85843 | 2016-04-15 00:00:00 | 6315 | 2016-04-15 00:05:00 | 6315 | 349 | 1 |
85844 | 2016-04-15 17:16:00 | 6315 | 2016-04-15 17:37:00 | 6315 | 1293 | 0 |
The query that I used to successfully answer the question was as follows, but how can I simplify it?:
SELECT
daily_trips_2016.avg_daily_trips AS avg_daily_trips_2016,
daily_trips_2017.avg_daily_trips AS avg_daily_trips_2017,
daily_trips_2016.month
FROM
(
SELECT -- This sub-query determines the average number of daily trips per month for the year 2016
ROUND(COUNT(*) / (COUNT(DISTINCT DAY(start_date))), 0) AS avg_daily_trips,
MONTH(start_date) AS month
FROM trips
WHERE YEAR(start_date) = 2016
GROUP BY month) AS daily_trips_2016
LEFT JOIN
(
SELECT -- This sub-query determines the average number of daily trips per month for the year 2017
ROUND(COUNT(*) / (COUNT(DISTINCT DAY(start_date))), 0) AS avg_daily_trips,
MONTH(start_date) AS month
FROM trips
WHERE YEAR(start_date) = 2017
GROUP BY month) AS daily_trips_2017
ON daily_trips_2016.month = daily_trips_2017.month; -- Since both sub-queries share the "month" column, the JOIN happens on this column
-- and we can see the daily averages side by side for each year
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用条件聚合:
Use conditional aggregation:
。Yyyymm /平均值1行
1 row by YYYYMM / average