简体   繁体   English

SQL - 使用开始日期计算每月平均收入

[英]SQL - Calculate average revenue per month using start date

A bit unfamiliar with this but wondering how I can use a similar function on SQL as this Google Sheets function to pull "average" monthly revenue based on the start_date a product started selling.对此有点不熟悉,但想知道如何在 SQL 上使用与此 Google Sheets 函数类似的函数来根据产品开始销售的 start_date 提取“平均”月收入。

=iferror(H200DATEDIF(VLOOKUP($A172,'DATA'!$A$2:$H$1000,13, false),TODAY(),"M"),0)

on SQL, if I were to want to pull the average monthly revenue from TODAY and by the number of months the product has been released, how would that look instead?在 SQL 上,如果我想从 TODAY 中提取平均月收入以及产品发布的月数,那会是怎样的结果?

So far, I have:到目前为止,我有:

SELECT 
 `product`.`id` AS `id`,
  DATE_FORMAT(`product`.start_selling_date, "%m/%d/%Y") AS `release_day`,
 `product`.`revenue`  as `rev`,
 FROM `product`

The goal would be to take the rev part which gives me total revenue and divide it by the number of months it's been since the release_day .目标是获取rev部分,它给我总收入,然后除以release_day以来的月数。

Example: Product A launched on 2022-01-21 and has $10,000 in revenue.示例:产品 A 于 2022 年 1 月 21 日推出,收入为 10,000 美元。 The average monthly would be around $1,428.57 (7 months).平均每月约为 1,428.57 美元(7 个月)。 If it's possible to have SQL calculate that automatically when pulling the rest of the information (product ID, release_day, revenue, average monthly revenue)如果有可能让 SQL 在提取其余信息(产品 ID、发布日期、收入、平均月收入)时自动计算

Is it maybe something like DATEDIFF?可能是 DATEDIFF 之类的东西吗?

Thank you!谢谢!

Yes, DATEDIFF() .是的, DATEDIFF() Assuming the date field is an actual date or datetime field, something like...假设日期字段是实际datedatetime时间字段,例如...

SELECT
product.revenue / ( DATEDIFF( CURDATE(), product.start_selling_date ) / 30 ) AS average_monthly_revenue

I am using 30 day months here, but you could get more accurate if needed.我在这里使用 30 天的月份,但如果需要,您可以得到更准确的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM