Once again it is time for a Phil Factor Speed Phreak Challenge and the prize is now a $100 Amazon voucher, and the privilege of displaying this rather nice trophy jpg on your blog / website / bedroom wall.
This time your task is simply to produce a summary report of the cost of international phone calls made within an organization on a week by week basis for both Users and Offices. You are allowed to use any method you see fit, you may also add any indexes, table-functions or views that you wish (though not an index view). Creation of these will not count to the overall execution time. If you are unsure if what you want to do might disqualify you, then please post a comment.
The table CallLog contains the log of every phone call, including which user called which number, when the call started and ended, and the office the user was in at the time. You will notice that it is not well normalized, since it is actually a view taken from several tables. Users are never in a fixed office, and can move from office to office at any point. Calls with a CallEnd equal to CallStart were not answered and can be safely ignored.
To calculate the cost of the call you need to use a lookup within the PhoneTariff table. The calls are prefixed by an area code corresponding to a country.
You must note that many of the call areas have the same starting character sequence , so calls made to numbers starting '35191' must be priced using the tariff of '35191' not '351'.
There is a rather elaborate charging system according to the length of the call. This means that you need to calculate the cost of the call as the minute by minute cost changes with the length of the call. These must be summed.
Looking at the PhoneTariffCharges table
The first 8 minutes of the call will be 0.4792 per minute. 9 to 31 minutes will be 0.18 per minute 32 to 59 minutes will be 0.5702 per minute etc.... All ranges have a UpToXMinutes of 9999 so you dont need to worry about an upper limit. The calllength is rounded upto the nearest whole minute.
Here is the DDL to create the tables
and heres the link to the data. Use
to load the data in.
Heres the solution provided by our mediocre developer Robert Bar, please note a fix for an issue with the week number being taken from @CallEnd.
Here are some guidelines for your entries:
1) Include a header in your suggestion. Make sure your name and the current date is present.
2) Include an edition number. First edition is 1. If you later improve your current suggestion post it again as version 2. Example: “Peso 1” and if improved, “Peso 1b”, “Peso 1c” etc.
3) If you are trying a new algorithm, change the edition to “Peso 2”. If you improve this algorithm, change the version to “Peso 2b”, “Peso 2c” etc. This will save Phil hours of work in the test harness!
4) The solution must clear up all its mess (temporary tables, indexes, etc.) so it can be re-run without errors.
As ever Phil Factor will be final judge.
The closing date will be midnight Thursday 17th December London
answered Sep 15 '10 at 09:24 AM
-----Author Sampath Natarajan
I would like to have a stab at this and future challenges also but I would like to know how to time my script.
What is the method that is being used?
answered Feb 09 '10 at 04:40 PM
Unfortunately I haven't had time to come up with a solution of my own... seems like it would be hard to compete with the ones provided so far though... some very nice coding.
One thing I would like to mention as I don't think it has been, is that some of the solutions (Peso v4 family for example) take advantage of the fact that the CallAreas exist for the duration of the reporting period, so while they do solve the problem at hand, they may not be suitable for a real solution.
I believe that if for example CallArea 35191 was not introduced as a separate phone tarrif until after the first call using it was made... rather than the call being charged at the 351 rate, it would not be charged at all. That is the call log would still match to the 35191 code, but when calculating the charge (connection mainly), there would be no record for it in the PhoneTariff matching the date range, causing the call to be 'forgotten' and not charged at all (some versions may just miss the connection charge).
Of course this is all irrelevant, if when a new CallArea is introduced that it gets a back-dated copy of the tariff it was derived from (in this case a copy back to the start of the records of the 351 rate), or if indeed the list we were given was a view that ensured all tariffs were valid for the whole of the reporting period.
For the problem at hand though, it's a nice trick that I'm sure sped up the results.
answered Dec 18 '09 at 12:46 AM
Figured out the various problems. I believe that some of the other solutions may not be including the ConnectionCharge in totals. Also was excluding about 10,000 UserNames because of the old datetime as a date + 00:00:00 not including if same date but time past midnight problem. Casted the CallStart to date before doing BETWEEN.
I used lmu92's same indexes (thanks!).
Same as lmu92's cleanup (thanks again!):