Hi @Santosh ,
Thanks for your update.
It is strongly recommended for you to provide enough sample data to illustrate all angles of the problem and the expected output. Then we could avoid guessing and correcting again and again.
Please refer below updated one:
drop table if exists reports, TestResultAvailibiltyMaster
create table reports
(branchname varchar(20),
testdate date)
insert into reports values
('Work_Branch','2021-05-30'),
('Work_Branch','2021-06-01'),
('Test_Branch','2021-05-31'),
('Test_Branch','2021-06-02')
create table TestResultAvailibiltyMaster
(branchname varchar(20),
testdate date,
ResultAvailability time)
insert into TestResultAvailibiltyMaster values
('Work_Branch','2021-06-01','21:10:10')
Method One using not exists:
insert into TestResultAvailibiltyMaster
select a.*,FORMAT(GETDATE(),'hh:mm:ss') ResultAvailability
from (select branchname,max(testdate) testdate from reports group by branchname) a
where not exists (select 1 from TestResultAvailibiltyMaster b where b.branchname=a.branchname and b.testdate=a.testdate)
Method Two using left join:
insert into TestResultAvailibiltyMaster
select a.*,FORMAT(GETDATE(),'hh:mm:ss') ResultAvailability
from (select branchname,max(testdate) testdate from reports group by branchname) a
left join TestResultAvailibiltyMaster b on b.branchname=a.branchname and b.testdate=a.testdate
where b.branchname is null
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.