An IVR application stores call information into a SQL Server table. A reporting application needs to read phone numbers from the call log and identify the country-code/area-code that is part of the phone number. Your job is to write a query that identifies the longest matching country/area code from each phone number based on the information stored in an area-code reference table.
Want to make SQL effortless?
With code-completion, SQL reformatting, script summaries and more, SQL Prompt 5 makes writing, editing, and exploring SQL effortless.
Download a 14-day free trial now.
Sample Data
PFX Destination
------ ---------------
1 USA
1204 Canada-Manitoba
124623 Barbados-Mobile
91 India
9179 Ahmedabad-India
DialedNumber
-------------
0012023525678
0012046613456
0012462311234
0091789960909
0091790909090
Expected Results
DialedNumber PFX
-------------- --------
0012023525678 1
0012046613456 1204
0012462311234 124623
0091789960909 91
0091790909090 9179
Rules
- If a given phone number matches with more than one area codes, then the longest should be selected.
- Output should be sorted by DialedNumber.
- There will be no duplicate numbers in the input data.
Sample Script
Use the TSQL script given below to generate the sample data for this challenge.
IF OBJECT_ID('TC54_CallLog') IS NOT NULL BEGIN
DROP TABLE TC54_CallLog
END
GO
CREATE TABLE TC54_CallLog (
CallLogID INT IDENTITY PRIMARY KEY,
DialedNumber VARCHAR(20)
)
INSERT INTO TC54_CallLog (DialedNumber)
SELECT '0012023525678' UNION ALL
SELECT '0012046613456' UNION ALL
SELECT '0012462311234' UNION ALL
SELECT '0091789960909' UNION ALL
SELECT '0091790909090'
GO
IF OBJECT_ID('TC54_Pfx') IS NOT NULL BEGIN
DROP TABLE TC54_Pfx
END
GO
CREATE TABLE TC54_Pfx (
Pfx VARCHAR(20),
Destination VARCHAR(20)
)
GO
INSERT INTO TC54_Pfx (Pfx, Destination)
SELECT '1','USA' UNION ALL
SELECT '1204','Canada-Manitoba' UNION ALL
SELECT '124623','Barbados-Mobile' UNION ALL
SELECT '91','India' UNION ALL
SELECT '9179','Ahmedabad-India'
SELECT * FROM TC54_Pfx
SELECT * FROM TC54_CallLog
Restrictions
- The solution should be a single query that starts with a "SELECT" or “;WITH”
Notes
- Read the Submission Guidelines and make sure that your solution follows them.
- If you would like to use a Tally Table, you can use the script given here. Your solution should not include the script to create and populate the tally table. You can assume that the tally table will be available in the database where the evaluation team will run your Code.
SQL Source Control - now with static data support
"By allowing static data to be versioned, SQL Source Control now becomes a complete source of truth for the application's data layer. I'm in." Troy Hunt, Software Architect.
More information and a free trial.
|
|