Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15889

SQL Update Required

$
0
0

Hi Experts ,

I have Two Tables EMp and Mast Table .

I need to Update ADCID of Emp Table with ID coulmn of Master Table where EffectiveDT of Emp Table is greater or Equal to
MAstEffective date of MAst Table .(Depending on Updateddate...Updatedate is when data is Uploaded\Upated in Database),
because new records keep coming in Mast Table with new Updated date with different MAstEffective

----------BELOW ARE SAMPLE TABLE
create table emp (USERID int,ADCID int,ComID int,classID int,EffectiveDT date )
insert into emp
select 1001,1,11,22,'2012-02-01'
Union all
select 1001,2,11,22,'2012-04-01'
Union all
select 1001,2,11,23,'2012-06-01'
union all
select 1001,10,11,24,'2012-08-01'
union all
select 1001,11,11,23,'2012-08-01'
union all
select 1002,2,11,23,'2012-08-01'
union all
select 1002,2,11,23,'2012-11-01'

---------------------
create Table MAst (Userid int, ID int,MAstEffective date,Updatedate date)
insert into MAst
select 1001,1,'2012-02-01','2012-02-02'
union all
select 1001,2,'2012-04-01','2012-05-01'
union all
select 1001,3,'2012-01-01','2012-06-01'
union all
select 1001,2,'2012-03-01','2012-07-01'
union all
select 1002,21,'2012-06-01','2012-06-02'
union all
select 1002,33,'2012-08-05','2012-09-02'
--------------------------------

Example

In above Table 1st REcord of MAST TABLE ,for EMPLOYEENO -1001 employee id =1 ,so ADCID of Emp Table should BE=1 for all Effective Date of EMPTable  >=
MAstEffective date of MAST Table
Then In NEXT record of MAST Table  for EMPLOYEENO 1001 employee id =2 ,SO ADCID of Emp Table should be=2 for all Effective Date of EMPTable >=
MAstEffective date of MAST Table

Then In NEXT record of MAST Table  for 1001 employee id =3 ,SO ADCID of Emp Table should =3 for all Effective Date of EMPTable >=
MAstEffective date of MAST Table

Then In NEXT record of MAST Table  for 1001 employee id =2 ,SO ADCID of Emp Table should =2 for all Effective Date of EMPTable >=
MAstEffective date of MAST Table

In This way I need to Update my EMP Table

Below is the Sample OutPut table How EMP Table Should Look Like after Update

create table OutPutemp (USERID int,ADCID int,ComID int,classID int,EffectiveDT date )
insert into OutPutemp
select 1001,3,11,22,'2012-02-01'
Union all
select 1001,2,11,22,'2012-04-01'
Union all
select 1001,2,11,23,'2012-06-01'
union all
select 1001,2,11,24,'2012-08-01'
union all
select 1001,2,11,23,'2012-08-01'
union all
select 1002,21,11,23,'2012-08-01'
union all
select 1002,33,11,23,'2012-11-01'


Viewing all articles
Browse latest Browse all 15889

Trending Articles