This one is quite tricky i’ve been scratching my head all day.
I have a table containing billing periods
ID | DATEBEGIN | DATEEND | CUSTOMER_ID
=======+===========+==========+=================
1 | 1/1/2011 | 30/1/2011 | 1
I have a table containing ‘sub customers’
ID | NAME
=======+===========
1 | JOHN
2 | JACK
3 | Elwood
I have a table containing items purchased on a subscription (wholesale account
ID | DATE | SUBCUSTOMER_ID | CUSTOMER ID
=======+===========+================+==============
1 | 15/1/2011 | 1 | 1
2 | 18/1/2011 | 1 | 1
3 | 25/1/2011 | 2 | 1
4 | 28/1/2011 | 3 | 1
So I want to count ‘credits’ to deduct from their account. So the subscription is per ‘sub customer’.
So at the end of the billing period (30/1/2011 from first table). I need to count the distinct sub customers (there are 3). They are charged pro-rata from the first purchase they make during the billing period.
Days Having Subscription | SUBCUSTOMER_ID | Pro Rata Rate | CUSTOMER_ID
==========================+===================+==================+==============
3 | 3 | 3/30 | 1
6 | 2 | 6/30 | 1
16 | 1 | 16/30 | 1
The output should therefore be
CUSTOMER_ID | BILLING CREDITS
============+========================
1 | 25/30
I have to count it pro rata, previously it would be unfair to bill a full period even if they purchase an item 1 day prior to the billing date
1 Answer