题目链接:点击打开链接
题目大意:计算连续每个季度的平均指标(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