This is a real life scenario I came across recently. We build applications
for the health care industry and one of the modules in the application generates
invoices for the services provided to the patients and sent them to the
insurance companies. The information is sent to the insurance companies as
electronic files. Each company have their own format and rules for the
electronic file they accept.
Before I explain the rest of the requirements, let me show you some sample
data.
InvID BatchNumber InvoiceNumber VisitDate Amount
------ ----------- ------------- ----------- ------
1 10000001 20001 2009-01-01 50.00
2 10000001 20001 2009-01-02 50.00
3 10000001 20001 2009-01-03 50.00
4 10000001 20001 2009-01-04 50.00
5 10000001 20002 2009-01-01 50.00
6 10000001 20002 2009-01-02 50.00
7 10000001 20002 2009-01-03 50.00
8 10000001 20003 2009-01-01 50.00
9 10000001 20003 2009-01-02 50.00
10 10000001 20004 2009-01-01 50.00
11 10000001 20004 2009-01-02 50.00
12 10000001 20004 2009-01-03 50.00
13 10000001 20004 2009-01-04 50.00
14 10000001 20004 2009-01-05 50.00
15 10000001 20004 2009-01-06 50.00
16 10000001 20005 2009-01-01 50.00
17 10000001 20005 2009-01-02 50.00
18 10000001 20005 2009-01-13 50.00
19 10000001 20005 2009-01-14 50.00
20 10000001 20005 2009-01-15 50.00
21 10000001 20005 2009-01-06 50.00
22 10000001 20005 2009-01-07 50.00
23 10000001 20005 2009-01-08 50.00
24 10000001 20006 2009-01-01 50.00
25 10000002 20007 2009-01-01 50.00
26 10000002 20007 2009-01-02 50.00
27 10000002 20007 2009-01-03 50.00
28 10000002 20008 2009-01-01 50.00
29 10000002 20008 2009-01-02 50.00
30 10000002 20008 2009-01-03 50.00
Let me start explaining the rules and make the requirement further clear. The
insurance company needs an electronic file in a certain format (the format is
irrelevant for us). A separate file needs to be generated for each batch. So
based on the sample data we need to generate two files (one for each batch).
Now comes the complex part. Each batch should contain not more than 10 rows.
The maximum number of lines we can put in a file is 10. So we need to break each
batch into parts that contains not more than 10 rows. When breaking the batches,
there is one more rule to follow. While breaking the batches, we should take
care that we do not break invoices.
The challenge here is to generate an additional column that helps us to break
the batches into pieces of 10 or lesser rows. Here is the expected output:
InvID BatchNumber InvoiceNumber VisitDate Amount Set
------ ----------- ------------- ----------- ------ ---
1 10000001 20001 2009-01-01 50.00 1
2 10000001 20001 2009-01-02 50.00 1
3 10000001 20001 2009-01-03 50.00 1
4 10000001 20001 2009-01-04 50.00 1
5 10000001 20002 2009-01-01 50.00 1
6 10000001 20002 2009-01-02 50.00 1
7 10000001 20002 2009-01-03 50.00 1
8 10000001 20003 2009-01-01 50.00 1
9 10000001 20003 2009-01-02 50.00 1
10 10000001 20004 2009-01-01 50.00 2
11 10000001 20004 2009-01-02 50.00 2
12 10000001 20004 2009-01-03 50.00 2
13 10000001 20004 2009-01-04 50.00 2
14 10000001 20004 2009-01-05 50.00 2
15 10000001 20004 2009-01-06 50.00 2
16 10000001 20005 2009-01-01 50.00 3
17 10000001 20005 2009-01-02 50.00 3
18 10000001 20005 2009-01-13 50.00 3
19 10000001 20005 2009-01-14 50.00 3
20 10000001 20005 2009-01-15 50.00 3
21 10000001 20005 2009-01-06 50.00 3
22 10000001 20005 2009-01-07 50.00 3
23 10000001 20005 2009-01-08 50.00 3
24 10000001 20006 2009-01-01 50.00 3
25 10000002 20007 2009-01-01 50.00 1
26 10000002 20007 2009-01-02 50.00 1
27 10000002 20007 2009-01-03 50.00 1
28 10000002 20008 2009-01-01 50.00 1
29 10000002 20008 2009-01-02 50.00 1
30 10000002 20008 2009-01-03 50.00 1
Sample Data
Use the following script to generate the sample data needed for this challenge
DECLARE @t TABLE (
InvID INT IDENTITY,
BatchNumber INT,
InvoiceNumber INT,
VisitDate DATETIME,
Amount MONEY )
INSERT INTO @t(BatchNumber, InvoiceNumber, VisitDate, Amount)
SELECT 10000001,20001,'2009-01-01',50.00 UNION ALL
SELECT 10000001,20001,'2009-01-02',50.00 UNION ALL
SELECT 10000001,20001,'2009-01-03',50.00 UNION ALL
SELECT 10000001,20001,'2009-01-04',50.00 UNION ALL
SELECT 10000001,20002,'2009-01-01',50.00 UNION ALL
SELECT 10000001,20002,'2009-01-02',50.00 UNION ALL
SELECT 10000001,20002,'2009-01-03',50.00 UNION ALL
SELECT 10000001,20003,'2009-01-01',50.00 UNION ALL
SELECT 10000001,20003,'2009-01-02',50.00 UNION ALL
SELECT 10000001,20004,'2009-01-01',50.00 UNION ALL
SELECT 10000001,20004,'2009-01-02',50.00 UNION ALL
SELECT 10000001,20004,'2009-01-03',50.00 UNION ALL
SELECT 10000001,20004,'2009-01-04',50.00 UNION ALL
SELECT 10000001,20004,'2009-01-05',50.00 UNION ALL
SELECT 10000001,20004,'2009-01-06',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-01',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-02',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-13',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-14',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-15',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-06',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-07',50.00 UNION ALL
SELECT 10000001,20005,'2009-01-08',50.00 UNION ALL
SELECT 10000001,20006,'2009-01-01',50.00 UNION ALL
SELECT 10000002,20007,'2009-01-01',50.00 UNION ALL
SELECT 10000002,20007,'2009-01-02',50.00 UNION ALL
SELECT 10000002,20007,'2009-01-03',50.00 UNION ALL
SELECT 10000002,20008,'2009-01-01',50.00 UNION ALL
SELECT 10000002,20008,'2009-01-02',50.00 UNION ALL
SELECT 10000002,20008,'2009-01-03',50.00
Notes
- Read the Submission Guidelines and make sure that your solution follows
them.
- The solution should work on SQL Server 2005, 2008 or later versions
- Last date to submit your entries: Monday 21 September 2009 Midnight GMT
- Use this forum for any
questions related to TSQL Challenge #13
About the Author.