LeetCode(数据库)- Hopper Company Queries III
Posted 程序员牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- Hopper Company Queries III相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:计算连续每个季度的平均指标(ride_distance、ride_duration)。
解题思路:略。
AC 代码
WITH RECURSIVE cal AS(
SELECT 1 l, 1 mon1, 2 mon2, 3 mon3
UNION ALL
SELECT l + 1, mon1 + 1, mon1 + 2, mon1 + 3 FROM cal WHERE mon1 < 10
),
t AS(SELECT a.ride_id, ride_distance, ride_duration, requested_at FROM Rides r JOIN AcceptedRides a ON r.ride_id = a.ride_id AND requested_at BETWEEN '2020-01-01' AND '2020-12-31')
SELECT l month, IFNULL(ROUND(SUM(ride_distance) / 3, 2), 0) average_ride_distance, IFNULL(ROUND(SUM(ride_duration) / 3, 2), 0) average_ride_duration
FROM cal LEFT JOIN t ON MONTH(requested_at) IN (mon1, mon2, mon3)
GROUP BY l
以上是关于LeetCode(数据库)- Hopper Company Queries III的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode(数据库)- Hopper Company Queries II