I think I over complicated the problem I am having that I described in these two threads:Foreign key assignment with set based insertForeign key assign (part 2 ... More Complicated)The first does not have a solution. There is no way to "discover' the foreign keys without adding extra columns.The second one should have a solution but the problem description was lost in a wall of text, and sql so that I am not surprised no one saw the question much less understood it. So I have simplified the problem.I have two tables (simplified from the tables in the other topics to remove extraneous stuff):CREATE TABLE Instructions( InstructionId int IDENTITY(1,1) PRIMARY KEY NOT NULL, GroupId int, OrderNumber int) -- ddl and dml for InputData at end of post (same table from other posts)insert into Instructions (GroupId,OrderNumber)select i.GroupID , i.OrderNumber from InputData i where i.EventType in ('N', 'C')CREATE TABLE ScriptDrugs( ScriptDrugId int IDENTITY(1,1) PRIMARY KEY NOT NULL, InstructionId int foreign key references Instructions(InstructionId) not null, GroupId int, OrderNumber int)
Now I want to insert records from InputData into ScriptDrugs with the correct InstructionId. So the question is how is the correct InstructionId selected. Instruction TableInstructionId GroupId OrderNumber------------- ----------- -----------4 5834 17 5834 45 5834 53 178248 16 178248 21 178248 32 178248 5Input DataGroupID OrderNumber EventType----------- ----------- ---------5834 1 N5834 2 R5834 3 R5834 4 C5834 5 C178248 1 N178248 2 C178248 3 C178248 4 R178248 5 C178248 6 R
I have color coded the lines that show which records from the InputData table should join with which record in the Instructions table.So why were the red rows linked? Note that the OrderNumber for the three red rows in the InputData are equal to or greater than the OrderNumber in the red row in the Instructions table but less than the OrderNumber in the green row. Or if you want to look at it from the EventType column: all Reorders (for a given group) map back to the most recent previous change or new order. New and Changed records (for a given group) map directly to the corresponding New or Changed record in the Instructions table.So I am trying to write the join to this insert statement:insert into ScriptDrugs (InstructionId,GroupId,OrderNumber)select i.InstructionId , d.GroupID , d.OrderNumber from InputData d join Instructions i on ??????????????????????????????
That will get me these rows in the ScriptDrugs tableScriptDrugId InsructionId GroupID OrderNumber------------ ------------ ----------- -----------<auto gen> 1 5834 1<auto gen> 1 5834 2<auto gen> 1 5834 3<auto gen> 7 5834 4<auto gen> 5 5834 5<auto gen> 3 178248 1<auto gen> 6 178248 2<auto gen> 1 178248 3<auto gen> 1 178248 4<auto gen> 2 178248 5<auto gen> 2 178248 6
LaurieInputData:CREATE TABLE InputData( [GroupID] [int] NULL, [OrderNumber] [int] NOT NULL, [EventType] [varchar](1) NULL, [StartDate] [datetime] NULL, [EndDate] [datetime] NULL) select 178248, 3, 'C', '2008-10-02 00:00:00.000', '2008-12-23 14:00:47.000' union allselect 178248, 5, 'C', '2009-03-16 00:00:00.000', '2009-06-14 00:00:00.000' union allselect 178248, 1, 'N', '2008-05-05 00:00:00.000', '2008-07-10 00:00:00.000' union allselect 5834, 3, 'R', '2010-01-19 16:57:06.000', '2010-01-26 00:00:00.000' union allselect 5834, 1, 'N', '2010-01-19 00:00:00.000', '2010-01-19 16:28:02.000' union allselect 5834, 5, 'C', '2010-03-02 00:00:00.000', '2010-03-09 00:00:00.000' union allselect 178248, 4, 'R', '2008-12-23 14:00:47.000', '2009-03-16 00:00:00.000' union allselect 178248, 6, 'R', '2009-06-17 14:19:08.000', '2009-09-15 00:00:00.000' union allselect 178248, 2, 'C', '2008-07-10 00:00:00.000', '2008-10-02 00:00:00.000' union allselect 5834, 2, 'R', '2010-01-19 16:28:02.000', '2010-01-19 16:57:06.000' union allselect 5834, 4, 'C', '2010-01-26 00:00:00.000', '2010-02-25 00:00:00.000'