Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
srinathb42
Starting Member
4 Posts |
Posted - 2014-11-10 : 17:48:43
|
Hi,I have two tables:Table A || Table BID (Primary Key) || Source_Cust_ID(allows multiple values) Fname || Fname Lname || Lnamegender || gender || Confirm_date || Source1(unique value) || Source2(unique value)Compare Table A with Table B(on A.ID,B.Source_Cust_ID,B.Source1,B.Confirm_date) and UPDATE B.confirm_date to getdate if everything is matched.Thanks in advance! |
|
redhat69
Starting Member
2 Posts |
Posted - 2014-11-10 : 18:46:29
|
So you want to match table A and Table BA.ID = B.Source_Cust_IDThen when this matches you want to update the field Confirm_date with the current time stamp? |
|
|
Muj9
Yak Posting Veteran
75 Posts |
Posted - 2014-11-11 : 07:54:11
|
update [TableB] set Confirm_date = GETDATE()where Source_Cust_ID in (SELECT a.[ID] from dbo.TableA a left join [dbo].[TableB] b on a.ID = b.Source_Cust_ID) |
|
|
srinathb42
Starting Member
4 Posts |
Posted - 2014-11-11 : 09:34:22
|
Thank you for your replyI have to compare all fields in Table A with all fields in Table B and if every thing matches then update B.confirm_date to getdate.And one Source_Cust_ID may have two confirm_date so have to update only the latest record in B table.quote: Originally posted by Muj9 update [TableB] set Confirm_date = GETDATE()where Source_Cust_ID in (SELECT a.[ID] from dbo.TableA a left join [dbo].[TableB] b on a.ID = b.Source_Cust_ID)
|
|
|
Muj9
Yak Posting Veteran
75 Posts |
Posted - 2014-11-12 : 07:27:44
|
Try this--Create Tables-----------------------create table TableA(ID int Primary key clustered,Fname varchar(50),Lname varchar(50),gender varchar(10))create table TableB (Source_Cust_ID int,Fname varchar(50),Lname varchar(50),gender varchar(10),Confirm_date datetime,Source1 varchar(10),Source2 varchar(10))--Insert Data into tableA---------------------------insert into TableAselect 1 as ID ,'Alex' as Fname ,'Rooney' as Lname ,'Male' as gender union allselect 2 as ID ,'Roberto' as Fname ,'Carlos' as Lname ,'Male' as gender union allselect 3 as ID ,'Pablo' as Fname ,'Aimar' as Lname ,'Male' as gender union allselect 4 as ID ,'Crespo' as Fname ,'Ken' as Lname ,'Male' as gender --Insert Data into tableB---------------------------insert into TableBselect 1 as ID ,'Alex' as Fname ,'Rooney' as Lname ,'Male' as gender , null as Confirm_date,'R4' as Source1 ,'B8'as Source2 union allselect 2 as ID ,'Roberto' as Fname ,'Carlos' as Lname ,'Male' as gender , null as Confirm_date,'R20' as Source1 ,'B21'as Source2 union allselect 3 as ID ,'Pablo' as Fname ,'Aimar' as Lname ,'Male' as gender , null as Confirm_date,'R33' as Source1 ,'B85'as Source2 union allselect 4 as ID ,'Crespo' as Fname ,'Ken' as Lname ,'Male' as gender , null as Confirm_date,'R75' as Source1 ,'B65'as Source2 union allselect 5 as ID ,'Julie' as Fname ,'Clark' as Lname ,'Female' as gender, null as Confirm_date,'R96' as Source1 ,'B98'as Source2 union allselect 6 as ID ,'Mal' as Fname ,'Maloy' as Lname ,'Female' as gender, null as Confirm_date,'R101' as Source1 ,'B102'as Source2 union allselect 7 as ID ,'Mali' as Fname ,'Maloyyy' as Lname ,'Female' as gender, null as Confirm_date,'Rb654' as Source1 ,'Bk456'as Source2 --update confirmdatesupdate Tableb set Confirm_date = GETDATE()--Check Tables-------------select * from TableAselect * from TableB--insert same data with different date---insert into TableBselect 1 as ID ,'Alex' as Fname ,'Rooney' as Lname ,'Male' as gender , null as Confirm_date,'R1' as Source1 ,'B2'as Source2 ---update dateupdate Tableb set Confirm_date = GETDATE()where Source_Cust_ID = 1 and Confirm_date is null---check tablesselect * from TableAselect * from TableB---create third table with most recent dates------;with cte as (selectROW_NUMBER() over (partition by Source_Cust_ID order by [Confirm_date] desc)rn, [Source_Cust_ID] ,[Fname] ,[Lname] ,[gender] ,[Confirm_date] ,[Source1] ,[Source2] FROM [dbo].[TableB]) select * into tableC from cte where rn = 1 ---check if recent date has be selected--------- select * from tableC --update table b -----------------------------------------update [TableB] set Confirm_date = GETDATE() where Source_Cust_ID in (SELECT a.[ID]from dbo.TableA aleft join [dbo].[TableC] b on a.ID = b.Source_Cust_ID and a.Fname = b.Fname and a.Lname= b.Lname and a.gender = b.gender ) and Confirm_date in ( SELECT a.Confirm_datefrom dbo.TableC aleft join tableB b on a.Fname = b.Fname and a.Lname = b.Lname and a.Confirm_date = b.Confirm_dateand a.Source1=b.Source1 and a.Source2 = b.Source2 where b.Source_Cust_ID is not null)and Source1 in ( SELECT a.Source1from dbo.TableC aleft join tableB b on a.Fname = b.Fname and a.Lname = b.Lname and a.Confirm_date = b.Confirm_dateand a.Source1=b.Source1 and a.Source2 = b.Source2 where b.Source_Cust_ID is not null)and Source2 in ( SELECT a.Source2from dbo.TableC aleft join tableB b on a.Fname = b.Fname and a.Lname = b.Lname and a.Confirm_date = b.Confirm_dateand a.Source1=b.Source1 and a.Source2 = b.Source2 where b.Source_Cust_ID is not null) --------------------------------------------------------------------- select * from tableB /* Drop table tableA Drop table tableB Drop table tableC */ |
|
|
|
|
|
|
|