简体   繁体   English

每天计算会员收入

[英]Calculating membership revenue per day

I have a membership table with the colums ID, Start_date, End_date, Duration, Value 我有一个成员表,具有列ID,开始日期,结束日期,持续时间,值

ID | Start_date | End_date | Duration | Value |
1  | 2012-08-12 |2012-09-12|    30    |   10  |
2  | 2011-05-22 |2013-05-22|   720    |  2000 |

and so on 等等

I want to turn it into two colums, one which has the date, each day of the year, and the other which has the sum of all Value/duration of memberships on that day. 我想把它变成两个列,一个列具有日期,一年中的每一天,另一个列具有该天所有成员的价值/期限的总和。

I eventually need to turn it into a per month value, giving me a clear picture of what revenues to expect in the future thanks to running memberships. 我最终需要将其转换为每月的价值,从而使我清楚地了解由于拥有会籍而在未来将获得什么样的收入。

Right now I did something like 现在我做了类似的事情

select 
sum(if("2012-01-01" between start_date and end_date, total_value/duration, null)) as "2012-01-01",
sum(if("2012-01-02" between start_date and end_date, total_value/duration, null)) as "2012-01-02",
[...]
sum(if("2013-12-31" between start_date and end_date, total_value/duration, null)) as "2013-12-31"

from MembershipsTable

/* 0 rows affected, 1 rows found. / *受影响0行,找到1行。 Duration for 1 query: 3,666 sec. 1个查询的持续时间:3,666秒。 */ * /

but I do not see how I could easily sum them up to give me a per-month value. 但我看不出如何轻松地将它们加起来以提供每月的价值。 I could just create a sum of the columns again, but would rather not have to type in novels of text 我可以再次创建各列的总和,但不想输入文字小说

Run time is not an issue for the current format 对于当前格式,运行时间不是问题

I need an output of the shape 我需要形状的输出

Month    | Sum    |
Jan 2012 |4500    |
Feb 2012 |4215,91 |

Where the sum is the sum of all memberships intersecting that period calculated by price per day*number of days the membership has in the month. 其中,总和是与该期间相交的所有成员资格的总和,计算方式为:每日价格*成员资格在当月的天数。

So if a membership starts on 12 Nov, Ends 11 Dec, has a duration of 30, value of 300, I want to add 300/30*daysInNov to month november, same for dec, giving me +190 for Nov, +110 for Dec I need a sum of all memberships in that way. 因此,如果会员资格从11月12日开始,至12月11日结束,有效期为30,值300,我想将300/30 * daysInNov添加到11月,与12月相同,给我11月+190,11月+110十二月我需要这种方式的所有成员加总。

Does anyone have any idea? 有人有什么主意吗?

This is a bit of an ugly hack, but I believe something like the below would work if I'm understanding your needs correctly. 这有点丑陋,但是我相信,如果我正确地理解了您的需求,下面的内容将会起作用。

First, create a tabled named month_days that has all the months with their start and end date. 首先,创建一个名为month_days的表,其中包含所有月份以及其开始和结束日期。 You would use this as a utility table to join against and calculate monthly totals. 您将使用它作为实用工具表来加入并计算每月总计。

month_days
start_date | last_date 
2012-01-01 | 2012-01-31
2012-02-01 | 2012-02-29

Then, execute a join and calculation something like this: 然后,执行联接和计算,如下所示:

select format(month_days.start_date, 'MMM yyyy') AS [Month],
       sum(case when (memberships.start_date > month_days.start_date AND memberships.end_date < month_days.end_date)
              then datediff(day, memberships.end_date, memberships.start_date) * (value/duration)
            when (memberships.start_date between month_days.start_date and month_days.end_date)
              then (datediff(day, month_days.end_date, memberships.start_date) + 1) * (value/duration)
            when (memberships.end_date between month_days.start_date and month_days.end_date)
              then datediff(day, memberships.end_date, month_days.start_date) * (value/duration)
            else (datediff(day, month_days.end_date, month_days.start_date) + 1) * (value/duration)
        end) total_value
from memberships
inner join month_days
on memberships.start_date < month_days.end_date
and memberships.end_date > month_days.start_date
group by month_days.start_date
order by month_days.start_date

There are many ways to create the month_days table that would achieve a similar effect. 有多种方法可以创建month_days表,从而可以达到类似的效果。

You could probably also write a stored procedure to iterate through the months for each record, populate a temp table (or table variable) with the monthly sums, then return the contents of the temp table. 您可能还可以编写一个存储过程,以遍历每个记录的月份,用每月的总和填充临时表(或表变量),然后返回临时表的内容。

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

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