I have four table like below: CREATE TABLE Party ( PartyID int identity(1,1)not null, PartyRegisteredDate datetime, MemberNo varchar(16), SubsNo varchar(9), PerNo varchar(2) ) CREATE TABLE Contract ( ContractID int identity(1,1)not null, PartyID int, MemberNo varchar(16), SubsNo varchar(9), PerNo varchar(2) , ContractStartDate datetime, ContractEndDate datetime) CREATE TABLE IndividualContract ( IndContractID int identity(1,1)not null, ContractID int, PartyID int, MemberNo varchar(16), SubsNo varchar(9), PerNo varchar(2) ,ContractStartDate datetime, ContractEndDate datetime ) CREATE TABLE Customer ( CustomerID int, IndContractID int, ContractID int, MemberNo varchar(16), SubsNo varchar(9), PerNo varchar(2)) The business Key is MemberNo, SubsNo, PerNo in all the 4 tables. Contract table has PartyID as foreignKey from Party table IndividualContract table has ContractID,PartyID as foreignKeys from Party table and Contract table. Customer table has IndContractID,ContractID as foreignKeys from IndividualContract table. CustomerID in Customer table is nothing but PartyID. IndividualContract and Customer tables should be one-one. --Below is the source data in #Source table Create Table #Source (MemberNo varchar(16), SubsNo varchar(9), PerNo varchar(2),PartyRegisteredDate datetime, ContractStartDate datetime, ContractEndDate datetime) Insert into #Source Select '054911', '079548709', '01', '2002-09-01 00:00:00.000', '2012-08-01 00:00:00.000', '9999-12-31 00:00:00.000' union all Select '824212', '091547708', '01', '1987-07-14 00:00:00.000', '1987-07-14 00:00:00.000', '2010-09-01 00:00:00.000' union all Select '864211', '041609336', '01', '1993-01-01 00:00:00.000', '1999-03-01 00:00:00.000', '1999-12-31 00:00:00.000' union all Select '864212', '113523275', '01', '1993-01-01 00:00:00.000', '1998-06-01 00:00:00.000', '1999-12-31 00:00:00.000' union all Select '824212', '079548709', '02', '1987-07-14 00:00:00.000', '2010-09-01 00:00:00.000', '2010-09-01 00:00:00.000' union all Select '864211', '113523275', '02', '1993-01-01 00:00:00.000', '1999-03-01 00:00:00.000', '1999-12-31 00:00:00.000' union all Select '864212', '041609336', '02', '1993-01-01 00:00:00.000', '1998-05-01 00:00:00.000', '1999-12-31 00:00:00.000' union all Select '864212', '091547708', '03', '1993-01-01 00:00:00.000', '1998-06-01 00:00:00.000', '1999-12-31 00:00:00.000' union all Select '867616', '041609336', '03', '1993-04-05 00:00:00.000', '1998-05-01 00:00:00.000', '1999-12-31 00:00:00.000' union all Select '867616', '113523275', '03', '1993-04-05 00:00:00.000', '1998-06-01 00:00:00.000', '1999-12-31 00:00:00.000' This source data should go to the above 4 tables. But the requirement is: 1. For one MemberNo there should be only one row in Party table. So in my case ,the party table should have only 5 rows like below. PartyID PartyRegisteredDate MemberNo SubsNo PerNo 1 1987-07-14 00:00:00.000 824212 091547708 01 2 1993-01-01 00:00:00.000 864211 041609336 01 3 1993-01-01 00:00:00.000 864212 113523275 01 4 1993-04-05 00:00:00.000 867616 041609336 03 5 2002-09-01 00:00:00.000 054911 079548709 01 2. The contract table should have only records with PerNo='01' for one MemberNo.So in my case this table should have like below. ContractID PartyID MemberNo SubsNo PerNo ContractStartDate ContractEndDate 1 1 824212 091547708 01 1987-07-14 00:00:00.000 2010-09-01 00:00:00.000 2 2 864211 041609336 01 1993-01-01 00:00:00.000 1999-12-31 00:00:00.000 3 3 864212 113523275 01 1993-01-01 00:00:00.000 1999-12-31 00:00:00.000 4 5 054911 079548709 01 2002-09-01 00:00:00.000 9999-12-31 00:00:00.000 3. The IndividualContract and Customer should have all the records from #Source table, as IndividualContract and Customer tables are one-one. 4. IndivuidialContract table should look like below: Here the ContractID is based on the SubsNo from Contract table. IndContractID ContractID PartyID MemberNo SubsNo PerNo ContractStartDate ContractEndDate 1 1 1 824212 091547708 01 1987-07-14 00:00:00.000 1988-09-30 00:00:00.000 2 4 1 824212 079548709 02 2010-09-01 00:00:00.000 2010-09-01 00:00:00.000 3 2 2 864211 041609336 01 1999-03-01 00:00:00.000 1999-03-01 00:00:00.000 4 3 2 864211 113523275 02 1999-03-01 00:00:00.000 1999-12-31 00:00:00.000 5 3 3 864212 113523275 01 1998-06-01 00:00:00.000 1999-12-31 00:00:00.000 6 2 3 864212 041609336 02 1998-05-01 00:00:00.000 1998-05-31 00:00:00.000 7 1 3 864212 091547708 03 1998-06-01 00:00:00.000 1998-06-01 00:00:00.000 8 2 4 867616 041609336 03 1998-05-01 00:00:00.000 1998-05-01 00:00:00.000 9 3 4 867616 113523275 03 1998-06-01 00:00:00.000 1999-12-31 00:00:00.000 10 4 5 054911 079548709 01 2012-08-01 00:00:00.000 9999-12-31 00:00:00.000 5. Customer table should look like below: IndContractId and ContractID for this table comes from IndivuidialContract table. CustomerID IndContractID ContractID MemberNo SubsNo PerNo 1 1 1 824212 091547708 01 1 2 4 824212 079548709 02 2 3 2 864211 041609336 01 2 4 3 864211 113523275 02 3 5 3 864212 113523275 01 3 6 2 864212 041609336 02 3 7 1 864212 091547708 03 4 8 2 867616 041609336 03 4 9 3 867616 113523275 03 5 10 4 054911 079548709 01
↧
SQL server Query help
↧