从第一项开始计数,忽略接下来 30 天内的计数

发布于 2024-12-21 21:02:04 字数 2261 浏览 0 评论 0原文

我有一组订阅,从购买第一个商品开始,每个订阅持续 30 天

PID    |     Date Purchased     |   User ID      | Owner ID
=======+========================+================+==============
1      |  1/1/2011              | 1              |  1
2      |  2/1/2011              | 1              |  1
3      |  19/1/2011             | 1              |  1
4      |  3/2/2011              | 1              |  1
5      |  5/1/2011              | 2              |  1
6      |  9/2/2011              | 2              |  1

我有一个包含计费周期的表

Owner ID | DATEBEGIN        | DATEEND  
=========+==================+============
1        | 1/2/2011         | 2/3/2011   

因此,对于每个所有者 ID,我需要将期间分类为 dateend 之前的 30 天间隔值。我需要从第一次购买开始按唯一用户 ID 计算购买次数,并“预订”接下来的 30 天(在此期间不计算),然后计算此后的第一次购买。

例如,2011 年 1 月 1 日的 PID (PurchaseID) 1 会在接下来的 30 天内“预订”,因此它们会被忽略。 但这属于上一个计费周期,剩余部分转入下一个计费周期 所以第一个是 3/30,下一个是 27/30(2/1/2011、19/1/2011 的销售被忽略,但 3/2/2011 没有预订,所以被计算

Owner ID   | User ID   | PeriodBegin     | PeriodEnd    | Usage    | Comment
===========+===========+=====+===========+===============+=========+================
1          | 1         | 3/12/2010       | 1/1/2011     | 3/30     | 
1          | 1         | 2/1/2011        | 1/2/2011     | 27/30    |
1          | 1         | 2/2/2011        | 4/3/2011     | 29/30    |
1          | 1         | 5/3/2011        | 4/4/2011     | 1/30     |
1          | 2         | 2/2/2011        | 4/3/2011     | 25/30    |
1          | 2         | 5/3/2011        | 4/4/2011     | 5/30     |

在内 )我将这些汇总到下一个中​​,

Owner ID  | PeriodBegin     | PeriodEnd    | Usage    |
==========+=================+==============+==========+
1         | 3/12/2010       | 1/1/2011     | 3/30     | 
1         | 2/1/2011        | 1/2/2011     | 27/30    |
1         | 2/2/2011        | 4/3/2011     | 54/30    |
1         | 5/3/2011        | 4/4/2011     | 6/30     |

所以我一直在试图弄清楚如何告诉MySQL忽略第一次购买后30天内的值,然后在这个“预订”期结束后计算这些值。

为了更容易理解,我用 mp3 订阅进行了类比。第一个表可以是从唱片公司到发行商的购买(每个发行商都有自己的所有者 ID 和每个最终用户的多个帐户(用户 ID)。 因此,在他们第一次购买 mp3 时,他们可以享受 30 天的订阅服务,可以无限下载 mp3。倒数第二个表是他们的计费时间表。因此,如果他们在计费周期结束前 1 天购买了一首歌曲,那么对一整天的 mp3 收取费用是不公平的,因此必须将其分成几部分,并在下个月收取余额。

I have a set of subscriptions which last 30 days each from the purchase of the first item during the period

PID    |     Date Purchased     |   User ID      | Owner ID
=======+========================+================+==============
1      |  1/1/2011              | 1              |  1
2      |  2/1/2011              | 1              |  1
3      |  19/1/2011             | 1              |  1
4      |  3/2/2011              | 1              |  1
5      |  5/1/2011              | 2              |  1
6      |  9/2/2011              | 2              |  1

I have a table containing a billing cycle

Owner ID | DATEBEGIN        | DATEEND  
=========+==================+============
1        | 1/2/2011         | 2/3/2011   

So for each owner id I would need to categorize periods into 30 day intervals before the dateend value. I need to count the purchases by unique user id's from the first purchase, and 'book' the next 30 days (don't count during this period), and count the first after that.

So for instance PID (PurchaseID) 1 on 1/1/2011, 'books' the next 30 days so they are ignored.
But this falls into a previous billing period, and the remainder is carried into the next
so 3/30 in the first, and 27/30 in the next, (sales on 2/1/2011, 19/1/2011 are ignored, but on 3/2/2011 isn't booked so it is counted)

Owner ID   | User ID   | PeriodBegin     | PeriodEnd    | Usage    | Comment
===========+===========+=====+===========+===============+=========+================
1          | 1         | 3/12/2010       | 1/1/2011     | 3/30     | 
1          | 1         | 2/1/2011        | 1/2/2011     | 27/30    |
1          | 1         | 2/2/2011        | 4/3/2011     | 29/30    |
1          | 1         | 5/3/2011        | 4/4/2011     | 1/30     |
1          | 2         | 2/2/2011        | 4/3/2011     | 25/30    |
1          | 2         | 5/3/2011        | 4/4/2011     | 5/30     |

Eventually I aggregate these into the next

Owner ID  | PeriodBegin     | PeriodEnd    | Usage    |
==========+=================+==============+==========+
1         | 3/12/2010       | 1/1/2011     | 3/30     | 
1         | 2/1/2011        | 1/2/2011     | 27/30    |
1         | 2/2/2011        | 4/3/2011     | 54/30    |
1         | 5/3/2011        | 4/4/2011     | 6/30     |

So I'm stuck trying to figure out how to tell MySQL to ignore the values within 30 days of the first purchase and then to count the ones after this 'booked' period ends.

To make it easier to understand I've made an analogy to mp3 subscriptions. The first table can be purchases from record company to distributors (which each have their own owner ids and multiple accounts for each end user (user id).
So within their first purchase of an mp3, they have a 30 days subscription service to download unlimited mp3s. The second last table is their billing schedule. So its unfair to charge a full days worth of mp3s if they buy a song 1 day before the billing period ends so it has to be split up into pieces, and the balance charged the following month.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文