473,666 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tricky time interval billing problem

I am currently working on a tricky problem at work. I googled around a
bit, but "time intervals" did not come up with anything useful.
Although I have some rough idea of how I could solve it, I still would
value some input.

I have information of (It has only couple dozen entries.)
ServiceNum, DollarCost

and a input database table in the form (several GBytes):
ClientNumber (CN), BeginDate(BD), EndDate(ED),
ServiceNumber_P urchased(SN)
--Date intervals are always [closed, closed]

The output is:
ClientNumber(CN ), BeginDate(BD), Enddate(ED), DollarCost(DC)

With the following requirements:
1) The input dates can be overlapping dates.
The output has to be non-overlapping "broken up" dates

Example: (assuming SN 1=$10 and SN 2=$20)
input (CN, BD, ED ,SN):
10, 1/1/2001,1/1/2005,1
10, 1/1/2002,1/1/2004,2

should result in:
output (CN, BD, ED, DC):
10, 1/1/2001, 12/31/2001, $10
10, 1/1/2002, 1/1/2004, $30
10, 1/2,2004, 1/1/2005, $10

2) if the same service is purchased twice for an interval it should be
billed only once
Example:
input:
10, 1/1/2001, 1/1/2005, 1
10, 1/1/2004, 1/1/2007, 1

output:
10, 1/1/2001, 1/1/2007, $10

Here are my thoughts about a solution so far:

1. fetch the input data sorted by clientNumber, Begindate, Enddate and
ServiceNumber

2. read the row, store as temporary good interval, then read another
row

3. if new begin-date is bigger than previous good interval end-date (or
previously saved end-date), output previous good interval, start new
good interval

4. else create new good interval entry with good interval begin-date
and current row begin-date, store good interval end-date into a list
with bisect module or something (so we always compare against smallest
end-date).

5. Use "bitwise or" on a service bitfield to add services and caluate
the total

As you can see my algorithm is a bit scetchy right now, but at this
point I am more interested to determine if the bisect module would be
the best way to approach the date interval problem or if I should use
some other data structure instead (stack, queue, set,...). I am working
on a Python proof of concept now. It is likely that the company will
want a C++ version of it later (yikes).

nes

Dec 5 '05 #1
2 1816
pr*******@latin mail.com wrote:
I am currently working on a tricky problem at work. I googled around a
bit, but "time intervals" did not come up with anything useful.
Although I have some rough idea of how I could solve it, I still would
value some input.

I have information of (It has only couple dozen entries.)
ServiceNum, DollarCost

and a input database table in the form (several GBytes):
ClientNumber (CN), BeginDate(BD), EndDate(ED),
ServiceNumber_P urchased(SN)
--Date intervals are always [closed, closed]

The output is:
ClientNumber(CN ), BeginDate(BD), Enddate(ED), DollarCost(DC)

With the following requirements:
1) The input dates can be overlapping dates.
The output has to be non-overlapping "broken up" dates

Example: (assuming SN 1=$10 and SN 2=$20)
input (CN, BD, ED ,SN):
10, 1/1/2001,1/1/2005,1
10, 1/1/2002,1/1/2004,2

should result in:
output (CN, BD, ED, DC):
10, 1/1/2001, 12/31/2001, $10
10, 1/1/2002, 1/1/2004, $30
10, 1/2,2004, 1/1/2005, $10

2) if the same service is purchased twice for an interval it should be
billed only once
Example:
input:
10, 1/1/2001, 1/1/2005, 1
10, 1/1/2004, 1/1/2007, 1

output:
10, 1/1/2001, 1/1/2007, $10

Here are my thoughts about a solution so far:

1. fetch the input data sorted by clientNumber, Begindate, Enddate and
ServiceNumber

2. read the row, store as temporary good interval, then read another
row

3. if new begin-date is bigger than previous good interval end-date (or
previously saved end-date), output previous good interval, start new
good interval

4. else create new good interval entry with good interval begin-date
and current row begin-date, store good interval end-date into a list
with bisect module or something (so we always compare against smallest
end-date).

5. Use "bitwise or" on a service bitfield to add services and caluate
the total

As you can see my algorithm is a bit scetchy right now, but at this
point I am more interested to determine if the bisect module would be
the best way to approach the date interval problem or if I should use
some other data structure instead (stack, queue, set,...). I am working
on a Python proof of concept now. It is likely that the company will
want a C++ version of it later (yikes).

nes

First of all, you need to use ordering to ensure that the database gives
you the most convenient order for processing, as this will make your
computations much easier. So I'd suggest sorting by clientNumber,
ServiceNumber, Begindate and Enddate. That way you can consider each
service separately.

Then all you need to do is accumulate the entries where the clientNumber
and ServiceNumber are the same and the start date of the second is less
than or equal to the end date of the first, repeatedly until either
there's no date overlap or a new service and/or client is started.

This shouldn't need any intermediate storage of results: if the row
you've just read can be used to extend the current range then extend it,
otherwise emit the current range and replace it with the new row.

Or is there something I've failed to understand about how you want to
process the data?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 6 '05 #2
> First of all, you need to use ordering to ensure that the database gives
you the most convenient order for processing, as this will make your
computations much easier. So I'd suggest sorting by clientNumber,
ServiceNumber, Begindate and Enddate. That way you can consider each
service separately.

Then all you need to do is accumulate the entries where the clientNumber
and ServiceNumber are the same and the start date of the second is less
than or equal to the end date of the first, repeatedly until either
there's no date overlap or a new service and/or client is started.

This shouldn't need any intermediate storage of results: if the row
you've just read can be used to extend the current range then extend it,
otherwise emit the current range and replace it with the new row.

Or is there something I've failed to understand about how you want to
process the data?

regards
Steve


Hi Steve, (btw I am the one that sent you the quotes at Pycon)

Hm, that would be an algorithm for requirement number 2. I do not
understand how it would help with respect to requirement 1. Notice that
by coincidence, in my examples the input data is already sorted as you
specified.

The real data is of course more messy than my analogy and I will post
it here so that nobody can accuse me of "feature creep". I hope I don't
totally confuse you now. Feel free to ignore it. The real output is not
100% correct either (that is why I am rewriting the program). Some of
the database in all its gory, ..eh glory:

INPUT
43756352|D|01/01/1999|09/30/2003|DCUD2B00|D CUD2B00|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756353|D|01/01/1999|09/30/2003|DCUD2B00|D CUD2B00|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756351|D|01/01/1999|09/02/2002|DCUD2B00|D CUD2B00|Y|A|437 56350|M|83516
|00374 |9048327561|000 1|
43756354|D|04/02/1999|09/30/2003|DCUD2B00|D CUD2B00|Y|A|437 56350|W|83516
|00374 |9048327561|000 1|
43756351|M|01/01/1999|03/31/1999|MARTPPG2|M ARTPPG2|Y|A|437 56350|M|83516
|00374 |9048327561|000 1|
43756352|M|01/01/1999|03/31/1999|MARTPPG2|M ARTPPG2|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756353|M|01/01/1999|03/31/1999|MARTPPG2|M ARTPPG2|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756351|M|04/01/1999|07/31/2002|MBCPG002|M BCPG002|Y|A|437 56350|M|83516
|00374 |9048327561|000 1|
43756352|M|04/01/1999|07/31/2002|MBCPG002|M BCPG002|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756353|M|04/01/1999|07/31/2002|MBCPG002|M BCPG002|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756354|M|04/02/1999|07/31/2002|MBCPG002|M BCPG002|Y|A|437 56350|W|83516
|00374 |9048327561|000 1|
43756352|M|08/01/2002|09/30/2003|MBP07305|M BP07305|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756353|M|08/01/2002|09/30/2003|MBP07305|M BP07305|Y|A|437 56350|D|83516
|00374 |9048327561|000 1|
43756354|M|08/01/2002|09/30/2003|MBP07305|M BP07305|Y|A|437 56350|W|83516
|00374 |9048327561|000 1|
43756351|M|08/01/2002|09/02/2002|MBP07305|M BP07305|Y|A|437 56350|M|83516
|00374 |9048327561|000 1|

OUTPUT
43756350|904832 7561|DCUD2B00|D |A|01/01/1999|04/01/1999|83516 |00374
|0001|Y|A|DCUD2 B00|
43756350|904832 7561|DCUD2B00|D |E|04/02/1999|09/30/2003|83516 |00374
|0001|Y|A|DCUD2 B00|
43756350|904832 7561|MARTPPG2|M |A|01/01/1999|03/31/1999|83516 |00374
|0001|Y|A|MARTP PG2|
43756350|904832 7561|MBCPG002|M |A|04/01/1999|07/31/2002|83516 |00374
|0001|Y|A|MBCPG 002|
43756350|904832 7561|MBP07305|M |A|08/01/2002|09/02/2002|83516 |00374
|0001|Y|A|MBP07 305|
43756350|904832 7561|MBP07305|M |E|09/03/2002|09/30/2003|83516 |00374
|0001|Y|A|MBP07 305|

CHEAT SHEET:
| (M) | (H,W) | (D,O,S) ||
+============== =============== =
| - | - | - || O |
+-----------------------------------------+
| - | - | X || G |
+-----------------------------------------+
| - | X | - || F |
+-----------------------------------------+
| - | X | X || E |
+-----------------------------------------+
| X | - | - || C |
+-----------------------------------------+
| X | - | X || D |
+-----------------------------------------+
| X | X | - || B |
+-----------------------------------------+
| X | X | X || A |
+-----------------------------------------+

regards,
Nes

Dec 6 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
24470
by: Andreas | last post by:
Hi! Is it possible to get a time event at a specific time, for instance eight a'clock? My program is running in the background and is minimized to the tray bar. If not, is there a smooth way to accomplish this in a different way? Best regards, Andreas Lundgren
2
5210
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much appreciated. TIA
9
3723
by: Steve Jorgensen | last post by:
Hi all, I'm working on the schema for a database that must represent data about stock & bond funds over time. My connundrum is that, for any of several dimension fields, including the fund name itself, the dimension may be represented in different ways over time, and may split or combine from one period to the next. When querying from the database for an arbitrary time period, I need the data to be rolled up to the smallest extent...
18
12798
by: Jeremy Weiss | last post by:
I'm trying to build a database that will handle the monthly billing needs of a small company. I'm charting everything out and here's what I see: table for customers sub table to track payments received. No biggie, right? Well, here's my problem. I don't know how to tell access to modify everyone's account balance each month. And I can't just always assume that their monthly bill is $16 just because their balance is $16. If I do that...
9
7266
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null; Let's declare a constant Int32 m_TimePeriod = 10000;
4
16928
by: archana | last post by:
Hi all, I want to develop one windows service in which i want to provide some scheduling facility. What i want is to take start time frm one xml file and then at that specified start time. Which timer should i use in windows service to start particular process at user specified time.
17
1867
by: joebenjamin | last post by:
This is a problem I was trying to help a few friends figure out for fun. I am not sure how to go about this, but there is something I am missing here. Here is what we want the output to be: Need to read in a time period from the keyboard (for example 1.2 seconds, 3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to print out the word “TICK” and then wait the designated time period and print the word “TICK” again....
0
1079
by: KevLe | last post by:
I'm building a log search function in c# for a certain management app and would like some help on the design how to solve this, here is my solution (on paper) so far: The log files are saved to disk, one file per date and one row "per logged event". Each row has a specific logged time (within the file's date) with different keys and values. The search filter query is built up via comboboxes and a datagridview. The datagridview is...
15
6420
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but but that only gave me difference in hours and only if the times were on the same day (after wrapping with date() function). TIA
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8780
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.