|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2007-08-26 : 12:33:56
|
| I have three tables:Registration:CREATE TABLE [dbo].[Registration]( [Registration_Key] [int] IDENTITY(1,1) NOT NULL, [Unique_ID] [char](16) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [DOB] [datetime] NOT NULL, [Gender] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED ( [Registration_Key] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]Insert into registration values ( 'ABC1001', '19760123', 'F' )Insert into registration values ( 'CDE1002', '19690221', 'M' )Insert into registration values ( 'GF678', '19800204', 'F' )Registration_key unique_id DOB Gender1 ABC1001 1/23/1976 12:00:00 AM F2 CDE1002 2/21/1969 12:00:00 AM M3 GF678 2/4/1980 12:00:00 AM FEpisodes:CREATE TABLE [dbo].[Episodes]( [Episode_Key] [int] IDENTITY(1,1) NOT NULL, [Registration_Key] [int] NOT NULL, [MCP_Key] [int] NOT NULL, [Enrollment_Date] [smalldatetime] NOT NULL, [Discharge_Date] [smalldatetime] NULL, CONSTRAINT [PK_Episodes] PRIMARY KEY CLUSTERED ( [Episode_Key] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[Episodes] WITH CHECK ADD CONSTRAINT [FK_Episodes_Registration] FOREIGN KEY([Registration_Key])REFERENCES [dbo].[Registration] ([Registration_Key])GOALTER TABLE [dbo].[Episodes] CHECK CONSTRAINT [FK_Episodes_Registration]Episode_key Registration_key MCP_key Assessment_date Discharge_key1 1 1001 1/14/2007 1/14/2007 7 1 1002 11/21/2006 12/23/2006 5 2 1002 11/2/2006 12/25/2006 6 3 1444 3/4/2007 NULL8 4 1001 3/1/2007 NULL9 3 1423 3/5/2007 NULL AndAssessment Table:CREATE TABLE [dbo].[Assessment]( [Assessment_Key] [int] IDENTITY(1,1) NOT NULL, [Episode_KEY] [int] NOT NULL, [Registration_Key] [int] NOT NULL, [DISABILITY] [tinyint] NULL, [Year] [int] NULL, CONSTRAINT [PK_Assessment] PRIMARY KEY CLUSTERED ( [Assessment_Key] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[Assessment] WITH CHECK ADD CONSTRAINT [FK_Assessment_Episodes] FOREIGN KEY([Episode_KEY])REFERENCES [dbo].[Episodes] ([Episode_Key])GOALTER TABLE [dbo].[Assessment] CHECK CONSTRAINT [FK_Assessment_Episodes]GOALTER TABLE [dbo].[Assessment] WITH CHECK ADD CONSTRAINT [FK_Assessment_Registration] FOREIGN KEY([Registration_Key])REFERENCES [dbo].[Registration] ([Registration_Key])GOALTER TABLE [dbo].[Assessment] CHECK CONSTRAINT [FK_Assessment_Registration]Assessmnet_key Episodes Registraton_key Disability year1 1 1 3 20072 5 2 6 20073 5 2 6 20074 7 1 4 20075 1 1 6 20076 6 3 7 2006Stage table that the original data is coming fromCREATE TABLE [dbo].[stage]( [unique_id] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [Dob] [datetime] NULL, [Gender] [char](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [mcp_key] [int] NULL, [assessment_date] [datetime] NULL, [reason_code] [int] NULL, [disability] [int] NULL, [year] [int] NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFInsert into stage values ( 'CC1001', '19660405', 'M', 1001, '20070301', 0,4, 2007 )Insert into stage values ( 'ABC1001', '19800723', 'F', 1001, '20070415', 2,4, 2007 )Insert into stage values ( 'GF678', '19800204', 'M', 1423, '20070305', 7,7, 2007 )I have to load the stage data to three different tables based on the complex logic. I need to process the row by row using a cursor to follow the business rule.We will insert the record registration à Episodes à AssessmentsHere is the main logic If the unique_id DOES NOT exist in the registration table. 1)Insert registration records to registration table ( unique record)2)Retrive registration_key ( identity) and insert Episode table with corresponding data3) Retrive Episode_key ( identity_key) and insert remaining information into assessment tableIf the unique_id DOES exist in the registration table,1) Get Registration_key and update registration information with new data2) Scan Episodes table to see there are existing eposide using registration_key and MCP_key where discharge_date is NULL (there are existing episodes : same registration_key , mcP_key where discharge_date is null compare to the STAGE data ) IF the above answer is NO, then DO the same thing as above 2)3) Retrive registration_key ( identity) and insert Episode table with corresponding dataRetrive Episode_key ( identity_key) and insert remaining information into assessment tableIF the above answer is YES ( there are existing episodes : same registration_key , mcP_key where discharge_date is null compare to the STAGE data) THENLook at the Reason_code, if the reason_code in 2,3,5,6 then update Assessment_date to Discharge_date .. and insert remaining the assessment information.If the reason code is NOT in 2,3,5,6 , just retrieve the episode_key and insert remaing information to Assessment table. As an examples :Here is the stage dataUnique_id DOB GENDER MCP_KEY Assessment_Date Reason_code Disability yearcc1001 4/5/1966 M 1001 3/1/2007 0 4 2007ABC1001 1/23/1980 F 1001 4/15/2007 2 4 2007GF678 2/4/1980 M 1423 3/5/2007 7 7 2007 We are going to start with the first row:Cc1001 does not exist in registration table , so we1)Insert registration records to registration table ( unique record)2)Retrive registration_key ( identity) and insert Episode table with corresponding data3) Retrive Episode_key ( identity_key) and insert remaining information into assessment tableNext row : ABC1001 is Exist in registration table , 1)so we update the new data with existing data ( in this case the new DOB) in registration table2) we scan the Episode table to see if there is registration_key and mcp_key where discharge_date is NULL. Yes there is one record in Episodes table ( registration_key = 1 and mcp_key = 1001 where discharge date is null),So THEN we dont create a new episodes , we just need to update the discharge date and insert assessment records. To be able to update the discharge date ,Look at the Reason_code, if the reason_code in 2,3,5,6 then update Assessment_date to Discharge_date .. and insert remaining the assessment information.IF the reason code is NOT in 2,3,5,6 , just retrieve the episode_key and insert remaing information to Assessment table. The reason_code is 2 in this case, so we set discharge_date as the assessment_date And add the remaining Assessment data into the assessment tableNext row: GF678It exists in the registration table so we update the registration informationAnd scan at the episodes table to see if there is an existing episodes records based on the registration_key and MCP_key .. there is registration_key = 3 but different MCP_key ( 1444) where discharge_date is null..SO we add new Episodes with registration_key from the registration table and ADD remaining Assessment data to the Assessment table.So the final output looks like this:Registration_key unique_id DOB Gnder1 ABC1001 07/23/1976 F2 CDE1002 2/21/1969 M3 GF678 2/4/1980 F4 cc1001 4/5/1966 MEpisodes tableEpisode_key Registration_key MCP_key Assessment_date Discharge_key1 1 1001 1/14/2007 1/14/2007 7 1 1002 11/21/2006 12/23/2006 5 2 1002 11/2/2006 12/25/2006 6 3 1444 3/4/2007 NULL8 4 1001 3/1/2007 NULL9 3 1423 3/5/2007 NULL Assessment: Assessmnet_key Episodes Registraton_key Disability year1 1 1 3 20072 5 2 6 20073 5 2 6 20074 7 1 4 20075 1 1 6 20076 6 3 7 20067 8 4 4 20078 1 1 4 20079 9 3 7 2007I hope i explain it well .. I am not sure how to write this complex logic inside of cursor..can you show me how? |
|