DDL and DML for the InputData table and DDL for the OutputData table at end post.I want to insert data from InputData into OutputData with one tricky rule.Description of InputDataThe input data consists of groups (denoted by GroupId) with a series of Events (Active, Change and Reorder). The order of the events is denoted by the OrderNumber column. A given event for a given group can have multiple records (as there can be multiple dosages for an order). The number of records for each event is in the NumDosages column. So (from the test data) we have this data for group 10641:InputId GroupID OrderNumber NumDosages event Dosage Schedule-------- -------- ----------- ----------- ------- ------------------------------ -----------------1 10641 1 2 Active - 0.5 mg, TAB, PO (2)ea QHS Bedtime2 10641 1 2 Active - 0.5 mg, TAB, PO (1)ea BID Twice a day3 10641 2 1 Change - 1 mg, TAB, PO (1)ea TID Three times a day4 10641 3 1 Reorder - 1 mg, TAB, PO (1)ea TID Three times a day5 10641 4 1 Reorder - 1 mg, TAB, PO (1)ea TID Three times a day6 10641 5 2 Change - 0.5 mg, TAB, PO (1)ea QAM Morning7 10641 5 2 Change - 1 mg, TAB, PO (1)ea QHS Bedtime
The first event (OrderNumber = 1) and last event (OrderNumber = 5) have two Dosages each (NumDosages = 2) so there are two rows for each event. The second, third and fourth events (OrderNumber = 2, 3 and 4) have one Dosage each (NumDosages = 1) so there is one row for each of these events.Rules for Insertion- Each record in InputData will be inserted into OutputData with the column Disabled set to null.
- (Here's the tricky one). If the immediately previous event (within a given group) had more dosages then the given event, insert another copy of the previous event's rows with the column Disabled set to 'Y'
So for the given group my desired results would be that the following rows are inserted into OutputData:InputId GroupID OrderNumber NumDosages event Dosage Schedule Disabled-------- -------- ----------- ----------- ------- ------------------------------ ------------------ --------1 10641 1 2 Active - 0.5 mg, TAB, PO (2)ea QHS Bedtime NULL2 10641 1 2 Active - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL1 10641 1 2 Active - 0.5 mg, TAB, PO (2)ea QHS Bedtime Y2 10641 1 2 Active - 0.5 mg, TAB, PO (1)ea BID Twice a day Y3 10641 2 1 Change - 1 mg, TAB, PO (1)ea TID Three times a day NULL4 10641 3 1 Reorder - 1 mg, TAB, PO (1)ea TID Three times a day NULL5 10641 4 1 Reorder - 1 mg, TAB, PO (1)ea TID Three times a day NULL6 10641 5 2 Change - 0.5 mg, TAB, PO (1)ea QAM Morning NULL7 10641 5 2 Change - 1 mg, TAB, PO (1)ea QHS Bedtime NULL
The bolded rows were inserted because the NumDosages for the first event (OrderNumber = 1) is greater than the NumDosages for the second event (OrderNumber = 2).My work so farI am thinking it will be two inserts statements. The first one is easy:Insert into OutputData (InputId,GroupID,OrderNumber,NumDosages,event,Dosage,Schedule)select * from InputData
Then there is the tricky part ????table ddl and dml for InputDataCREATE TABLE OutputData( InputId int NOT NULL, GroupID int NOT NULL, OrderNumber int NOT NULL, NumDosages int NOT NULL, event nvarchar(7) NOT NULL, Dosage nvarchar(35) NOT NULL, Schedule nvarchar(20) NOT NULL, Disabled nvarchar(1) NULL) CREATE TABLE InputData( InputId int NOT NULL, GroupID int NOT NULL, OrderNumber int NOT NULL, NumDosages int NOT NULL, event nvarchar(7) NOT NULL, Dosage nvarchar(35) NOT NULL, Schedule nvarchar(20) NOT NULL) Insert into InputData (InputId,GroupID,OrderNumber,NumDosages,event,Dosage,Schedule)SELECT 1,10641,1,2,'Active',' - 0.5 mg, TAB, PO (2)ea QHS','Bedtime' UNION ALLSELECT 2,10641,1,2,'Active',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 3,10641,2,1,'Change',' - 1 mg, TAB, PO (1)ea TID','Three times a day' UNION ALLSELECT 4,10641,3,1,'Reorder',' - 1 mg, TAB, PO (1)ea TID','Three times a day' UNION ALLSELECT 5,10641,4,1,'Reorder',' - 1 mg, TAB, PO (1)ea TID','Three times a day' UNION ALLSELECT 6,10641,5,2,'Change',' - 0.5 mg, TAB, PO (1)ea QAM','Morning' UNION ALLSELECT 7,10641,5,2,'Change',' - 1 mg, TAB, PO (1)ea QHS','Bedtime' UNION ALLSELECT 8,14089,1,1,'Active',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 9,14089,2,3,'Change',' - 0.5 mg, TAB, PO (1)ea QAM','Morning' UNION ALLSELECT 10,14089,2,3,'Change',' - 0.5 mg, TAB, PO (1)ea Qnoon','Daily at Noon' UNION ALLSELECT 11,14089,2,3,'Change',' - 0.5 mg, TAB, PO (1)ea QHS','Bedtime' UNION ALLSELECT 12,14089,3,1,'Change',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 13,14089,4,1,'Reorder',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 14,14089,5,2,'Change',' - 0.25 mg, TAB, PO (1)ea Qnoon','Daily at Noon' UNION ALLSELECT 15,14089,5,2,'Change',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 16,14089,6,2,'Change',' - 0.25 mg, TAB, PO (1)ea Qnoon','Daily at Noon' UNION ALLSELECT 17,14089,6,2,'Change',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 18,14089,7,2,'Reorder',' - 0.25 mg, TAB, PO (1)ea Qnoon','Daily at Noon' UNION ALLSELECT 19,14089,7,2,'Reorder',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 20,14089,8,2,'Reorder',' - 0.25 mg, TAB, PO (1)ea Qnoon','Daily at Noon' UNION ALLSELECT 21,14089,8,2,'Reorder',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 22,14089,9,3,'Change',' - 0.25 mg, TAB, PO (1)ea QAM','Morning' UNION ALLSELECT 23,14089,9,3,'Change',' - 0.25 mg, TAB, PO (1)ea Qnoon','Daily at Noon' UNION ALLSELECT 24,14089,9,3,'Change',' - 0.5 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 25,15134,1,2,'Active',' - 25 mg, TAB, PO (1)ea QAM','Morning' UNION ALLSELECT 26,15134,1,2,'Active',' - 25 mg, TAB, PO (1)ea QHS','Bedtime' UNION ALLSELECT 27,15134,2,1,'Change',' - 25 mg, TAB, PO (1)ea TID','Three times a day' UNION ALLSELECT 28,15134,3,3,'Change',' - 25 mg, TAB, PO (2)ea QAM','Morning' UNION ALLSELECT 29,15134,3,3,'Change',' - 25 mg, TAB, PO (2)ea Qnoon','Daily at Noon' UNION ALLSELECT 30,15134,3,3,'Change',' - 25 mg, TAB, PO (1)ea QHS','Bedtime' UNION ALLSELECT 31,18157,1,2,'Active',' - 0.5 mg, TAB, PO (1)ea QAM','Morning' UNION ALLSELECT 32,18157,1,2,'Active',' - 1 mg, TAB, PO (1)ea QHS','Bedtime' UNION ALLSELECT 33,18157,2,1,'Change',' - 1 mg, TAB, PO (1)ea BID','Twice a day' UNION ALLSELECT 34,18157,3,2,'Change',' - 0.5 mg, TAB, PO (1)ea QAM','Morning' UNION ALLSELECT 35,18157,3,2,'Change',' - 1 mg, TAB, PO (1)ea QHS','Bedtime' UNION ALLSELECT 36,18157,4,1,'Change',' - 1 mg, TAB, PO (1)ea BID','Twice a day'
Expected Results from all test data (if I got it right)InputId GroupID OrderNumber NumDosages event Dosage Schedule Disabled-------- -------- ----------- ----------- ------- -------------------------------- -------------------- --------1 10641 1 2 Active - 0.5 mg, TAB, PO (2)ea QHS Bedtime NULL2 10641 1 2 Active - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL1 10641 1 2 Active - 0.5 mg, TAB, PO (2)ea QHS Bedtime Y2 10641 1 2 Active - 0.5 mg, TAB, PO (1)ea BID Twice a day Y3 10641 2 1 Change - 1 mg, TAB, PO (1)ea TID Three times a day NULL4 10641 3 1 Reorder - 1 mg, TAB, PO (1)ea TID Three times a day NULL5 10641 4 1 Reorder - 1 mg, TAB, PO (1)ea TID Three times a day NULL6 10641 5 2 Change - 0.5 mg, TAB, PO (1)ea QAM Morning NULL7 10641 5 2 Change - 1 mg, TAB, PO (1)ea QHS Bedtime NULL8 14089 1 1 Active - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL9 14089 2 3 Change - 0.5 mg, TAB, PO (1)ea QAM Morning NULL10 14089 2 3 Change - 0.5 mg, TAB, PO (1)ea Qnoon Daily at Noon NULL11 14089 2 3 Change - 0.5 mg, TAB, PO (1)ea QHS Bedtime NULL9 14089 2 3 Change - 0.5 mg, TAB, PO (1)ea QAM Morning Y10 14089 2 3 Change - 0.5 mg, TAB, PO (1)ea Qnoon Daily at Noon Y11 14089 2 3 Change - 0.5 mg, TAB, PO (1)ea QHS Bedtime Y12 14089 3 1 Change - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL13 14089 4 1 Reorder - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL14 14089 5 2 Change - 0.25 mg, TAB, PO (1)ea Qnoon Daily at Noon NULL15 14089 5 2 Change - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL16 14089 6 2 Change - 0.25 mg, TAB, PO (1)ea Qnoon Daily at Noon NULL17 14089 6 2 Change - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL18 14089 7 2 Reorder - 0.25 mg, TAB, PO (1)ea Qnoon Daily at Noon NULL19 14089 7 2 Reorder - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL20 14089 8 2 Reorder - 0.25 mg, TAB, PO (1)ea Qnoon Daily at Noon NULL21 14089 8 2 Reorder - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL22 14089 9 3 Change - 0.25 mg, TAB, PO (1)ea QAM Morning NULL23 14089 9 3 Change - 0.25 mg, TAB, PO (1)ea Qnoon Daily at Noon NULL24 14089 9 3 Change - 0.5 mg, TAB, PO (1)ea BID Twice a day NULL25 15134 1 2 Active - 25 mg, TAB, PO (1)ea QAM Morning NULL26 15134 1 2 Active - 25 mg, TAB, PO (1)ea QHS Bedtime NULL25 15134 1 2 Active - 25 mg, TAB, PO (1)ea QAM Morning Y26 15134 1 2 Active - 25 mg, TAB, PO (1)ea QHS Bedtime Y27 15134 2 1 Change - 25 mg, TAB, PO (1)ea TID Three times a day NULL28 15134 3 3 Change - 25 mg, TAB, PO (2)ea QAM Morning NULL29 15134 3 3 Change - 25 mg, TAB, PO (2)ea Qnoon Daily at Noon NULL30 15134 3 3 Change - 25 mg, TAB, PO (1)ea QHS Bedtime NULL31 18157 1 2 Active - 0.5 mg, TAB, PO (1)ea QAM Morning NULL32 18157 1 2 Active - 1 mg, TAB, PO (1)ea QHS Bedtime NULL31 18157 1 2 Active - 0.5 mg, TAB, PO (1)ea QAM Morning Y32 18157 1 2 Active - 1 mg, TAB, PO (1)ea QHS Bedtime Y33 18157 2 1 Change - 1 mg, TAB, PO (1)ea BID Twice a day NULL34 18157 3 2 Change - 0.5 mg, TAB, PO (1)ea QAM Morning NULL33 18157 3 2 Change - 1 mg, TAB, PO (1)ea QHS Bedtime NULL34 18157 3 2 Change - 0.5 mg, TAB, PO (1)ea QAM Morning Y35 18157 3 2 Change - 1 mg, TAB, PO (1)ea QHS Bedtime Y36 18157 4 1 Change - 1 mg, TAB, PO (1)ea BID Twice a day NULL
Note: all records with Disabled = Y would be inserted via rule 2.