[英]Calculating average time between dates in SQL
Using MySQL, I'm trying to figure out how to answer the question: What is the average number of months between users creating their Nth project?使用 MySQL,我试图弄清楚如何回答这个问题:创建第 N 个项目的用户之间的平均月数是多少?
Expected result:预期结果:
| project count | Average # months |
| 1 | 0 | # On average, it took 0 months to create the first project (nothing to compare to)
| 2 | 12 | # On average, it takes a user 12 months to create their second project
| 3 | 3 | # On average, it takes a user 3 months to create their third project
My MySQL table represents projects created by users.我的 MySQL 表代表用户创建的项目。 The table can be summarized as:
该表可以概括为:
| user_id | project created at |
|---------|--------------------|
| 1 | Jan 1, 2020 1:00 pm|
| 1 | Feb 2, 2020 3:45 am|
| 1 | Nov 6, 2020 0:01 am|
| 1 | Mar 4, 2021 5:01 pm|
|------------------------------|
| 2 | Another timestamp |
| 2 | Another timestamp |
| 2 | Another timestamp |
| 2 | Another timestamp |
| 2 | Another timestamp |
| 2 | Another timestamp |
|------------------------------|
| ... | Another timestamp |
| ... | Another timestamp |
Some users will have one project while some may have hundreds.有些用户只有一个项目,而有些用户可能有数百个。
with
paid_self_serve_projects_presentation as (
select
`Paid Projects`.owner_email
`Owner Email`,
row_number() over (partition by `Paid Projects`.owner_uuid order by created_at)
`Project Count`,
day(`Paid Projects`.created_at)
`Created Day`,
month(`Paid Projects`.created_at)
`Created Month`,
year(`Paid Projects`.created_at)
`Created Year`,
`Paid Projects`.created_at
`Created`
from self_service_paid_projects as `Paid Projects`
order by `Paid Projects`.owner_uuid, `Paid Projects`.created_at
)
select `Projects`.* from paid_self_serve_projects_presentation as `Projects`
You can use window functions.您可以使用窗口函数。 I am thinking
row_number()
to enumerate the projects of each user ordered by creation date, and lag()
to get the date when the previous project was created:我在想
row_number()
枚举按创建日期排序的每个用户的项目,并用lag()
获取上一个项目的创建日期:
select rn, avg(datediff(created_at, lag_created_at)) avg_diff_days
from (
select t.*,
row_number() over(partition by user_id order by created_at) rn,
lag(created_at, 1, created_at) over(partition by user_id order by created_at) lag_created_at
from mytable t
) t
group by rn
This gives you the average difference in days, which is somehow more accurates that months.这为您提供了平均天数差异,这在某种程度上更准确。 If you really want months, then use
timestampdiff(month, lag_created_at, created_at)
instead of datediff()
- but be aware that the function returns an integer value, hence there is a loss of precision.如果你真的想要几个月,那么使用
timestampdiff(month, lag_created_at, created_at)
而不是datediff()
- 但要注意函数返回一个整数值,因此有精度损失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.