Hi,
I have tmpCompensation that contains some data imported from some source. The file contains information including customernumber and deliverydate. If table Compensation contains the same customernumber with same deliverydate then I just need to update the row. Otherwise, insert a new row. The problem is that for customer 7865343 the row is updated but also a new row is added as it compares also the date 2017-03-04 with the date in the tmp table and since the date is different it also adds a new row with the date 2020-12-10. This shouldn't happen as 2020-12-10 for that customer exists in Compensation table so it should only update the row in Compensation. How can I achieve this?
The resultset after the update/insert should be as follows in the
compensation table
7865343, 2020-12-10,'bike','087543'
7865343, 2017-03-04,'home','864380'
95345, 2018-12-10,'home','864380'
95345, 2019-12-10,'home','864383'
Thanks
create table tmpCompensation (customerNumber int, deliveryDate varchar (20), deliveryMethod varchar(50), recid bigint)
create table Compensation (customerNumber int, deliveryDate varchar (20), deliveryMethod varchar(50), recid bigint)
insert into tmpCompensation select 434353, 2020-10-10,'bike','76756454' union
select 7865343, 2020-12-10,'bike','087543' union
select 95345, 2019-12-10,'home','864380'
insert into Compensation select 434353, 2020-10-10,'bike','76756454' union
select 7865343, 2020-12-10,'car','087543' union
select 7865343, 2017-03-04,'home','864380' union
select 95345, 2018-12-10,'home','864383'