i have a case where i need to compare snapshoted data(dbo.src) with destination table(dbo.dest) and find out which of the countries are new and which ones are not valid anymore.
DATA:
create table dbo.src
(
MessageID int not null
,ProductID int not null
,Country varchar(2) null
,ChangeDate date not null
)
insert into dbo.src
(MessageID,ProductID, Country, ChangeDate)
values
(1,1,'AT','2021-10-05')
,(2,1,'AT','2021-10-10')
,(2,1,'CH','2021-10-10')
,(3,1,'CH','2021-10-20')
create table dbo.dest
(
ProductID int not null
,Country varchar(2) null
,ChangeDate date not null
,Active varchar(1) not null
)
insert into dbo.dest
(ProductID, Country, ChangeDate,Active)
values
(1,'DE','2021-10-02','Y')
In dbo.dest table I already have one record with country DE and i have to compare it with the records in dbo.src in an exact way in which data arrived in source table(This can be tracked via column MessageID). Multiple changes arrived during some time period for the same product but different country setup.
The desired output would look like this:
Here is how to interpret desired results:
- In the 1st run we compared row from dbo.src with messageId=1 with current record in table dbo.dest. There is no more “DE” so we insert new record in dbo.dest with flag Active=N and put the date 2021-10-05 which would be some form of date when it became active. We also insert new record AT which would mean that new country arrived and became active.
- In the 2nd run, we would compare rows from dbo.src with MessageID=2 and the last active record with latest ChangeDate and do the same comparison like in previous step. The result would be that we insert only CH with date 2021-10-10 and do nothing for AT record since its still coming from dbo.src table.
- In the 3rd run we do the same comparisons like in previous 2 steps. The result would be inserting only new record for AT with active=N and date 2021-10-20 since this record no longer exists in dbo.src table where MessageID=3
How can this be done without cursors or loops?
Country = AT
withChangeDate = 2021-10-20
not active?...Is it because the older one on2021-10-05
was already marked active? Was the older one on2021-10-05
only marked active becauseDE
is no longer in thesrc
table? What if there were no rows in thedest
table, wouldCountry = AT
withChangeDate = 2021-10-05
still be marked active? Why isCountry = CH
withChangeDate = 2021-10-20
not in your final results?Country = AT
withChangeDate = 2021-10-20
not active? (It is in the followingMessageId = 2
). Also why isCountry = CH
withChangeDate = 2021-10-20
completely omitted from the final results? (It is neither listed as active or not active.)