我有一个id列表和他们旅行的详细信息。我想知道每个ID每天平均旅行多少次,但我不知道如何编写这个查询。表中的数据是这样的:
<表类>
ID
Ride_id
日期
tbody><<tr>1 123 2022-3-4 1124 2022-3-4 1111 2021-2-8 2584 2019-4-18 2256 2019-4-18 2805 2020-5-8 2127 2020-5-8 2457 2020-5-8 3 100 2021-4-7 3 101 2021-4-7 3 202 2021-5-17 3 741 2021-5-17 表类>
首先计算每个id每天的出行次数,然后对这些次数求平均值。
select id, avg(trips)
from (select id, count(*) as trips
from trips
-- where id in(1,2,3)
group by id, date) t
group by id
如果需要,您可以取消子查询中的where
子句的注释,以过滤特定的id…