|
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
(comments are locked)
|
|
I opted to go no hardwiring at all, because you never know when CallArea changes and suddenly has 6 digits, or more... First, the Setup part
And then the Teardown part
And finally the first version of the query
(comments are locked)
|
|
Peso 4c - 20091216 Same indexes as the last ones ?
Dec 17 '09 at 03:52 AM
dave ballantyne
Yes. Sorry about that. Same indexes as the version 4 collection.
Dec 17 '09 at 04:11 AM
Peso
I get this coming in at 3.5 seconds.
Dec 17 '09 at 05:49 AM
Phil Factor
And you get 4b at 3.0 seconds?
Dec 17 '09 at 05:53 AM
Peso
(comments are locked)
|
|
Phil Factor 1b (1a got mangled by the website because it has < in the code) This is my first entry, just to show a fairly conventional way of doing it. The call table is redone in a slightly more compute-friendly form. The same is done for the PhoneTariff table. Then the international call prefixes are identified by updating the table progressively, starting with the longest codes first and, at the same time, the PhoneTarrif and the initial connection charge is identified. Then, the calls are costed out, a charge-band at a time until the longest calls have been costed out. Once this has been done, then it is a simple matter of aggregating the reports. I've left my timing harness in place in case you want to tweak the solution, or if you want to see how I generally do it. On my server, I get times in the 7.5 sec range, for the whole operation. Last time I checked the result was the same as Robert Barr's (bless him) Oops. the first time I pasted the code in, I use but because I had a < in the code, it got mangled. Is there an easy way to past code into this darned software?
Dec 05 '09 at 07:21 PM
Phil Factor
Phil, I get Abel Buck included in your code. He is not to be reported since the call was not answered (CallStart equals CallEnd).
Dec 05 '09 at 07:37 PM
Peso
(comments are locked)
|
|
Here's what I came up with (so far...) In terms of performance I'm in a range of less than 5sec (including output of the results which takes about 0.6 sec). Just to compare: I'm getting approx. 8sec for Phils solution on my PC (thank you for leaving the timing harness in there!).
(comments are locked)
|
Nice! Half the challenge is to understand the execution plans and the reason they behave differently on Dave's and your's machine.
Dec 17 '09 at 05:55 AM
Peso
Results dont seem right , Arlene Frey's Total = 0.00 it should be 0.9246
Dec 17 '09 at 06:06 AM
dave ballantyne
Oops, forgot to re-check after making some changes. I was just testing to see if a test for null was slower than a test for 0 and forgot to change back! (it wasn't- another myth busted)
Dec 17 '09 at 06:18 AM
Phil Factor
Different , but still wrong :) , Arlene Frey now equals 0.3446
Dec 17 '09 at 07:54 AM
dave ballantyne
Fixed. Sorry, i was in a hurry this morning!
Dec 17 '09 at 10:17 AM
Phil Factor
(comments are locked)
|
« previous 1 2 3 4 next page »


Can we be specific about how timings will be done? Average of x runs with a dropcleanbuffers before each? Average of x runs with a dropcleanbuffers before the first run?
What about Abel Buck? His call was 0.000 seconds and thus no charge for length of call, but is he still going to pay for the connection of 52 cents?
I am tryting to find a 100% correct answer but sometimes i doubt if the answer privided by Robert Bar is 100% Correct. Should we take his answer as the final results or should we try to prove that there could be something wrong with it and propose something new ?
With regard to Abel Buck the call was not answered and therefore no charge. "Calls with a CallEnd equal to CallStart were not answered and can be safely ignored." The Robert Bar solution should be correct. If there are any issues with a calculation please PM me and ill manually double check.
I took a random case, number dialled: 980913853342, according to Robert Bar he used area code 980 and number 91-385-3342 (formated 2,3,4) ["order by len(CallArea) desc"], but you could also get area code 98 and number dialled 091-385-3342 ( formated 3-3-4 ). My point is that phone numbers of 12 digits ( this case ) should have just 2 chars on area code, those with 13 digits, have 3 chars as area code and so on, so the remaning number will still be formated 3-3-4 and not [1..3]-3-4. Meaning that the proposed solution could not be the corret one...