0

-- Source

Create table staging
(id int,
 name varchar(50),
 empid int,
 company_id int,
 addres varchar(50)
)

Create table Destination
(id int,
 name varchar(50),
 empid int,
 company_id int,
 addres varchar(50)
)

insert into staging
select 1, 'amit',NULL,101,'USA'
UNION ALL
Select 1,'amit',10002,'','USA'
UNION ALL
Select 2,'Vijay','',650,'China'
UNION ALL
Select 2,'Vijay','','','China'
UNION ALL
Select 5,'Sanjay',589756,NULL,'India'
UNION ALL
Select 5,'Sanjay',NULL,151215,'India'


Select * from staging

-- Expected result

-- Destination table

id  name    empid   company_id  addres
1   amit    10002   101         USA
2   Vijay   0       650         China
5   Sanjay  589756  151215      India
2
  • Please, edit your question by adding the expected result. Commented Dec 17, 2016 at 8:53
  • It would look like GROUP BY is the solution, though the question is if, for instance, you may have a record like 1,'amit',44444,'','USA'. Commented Dec 17, 2016 at 11:17

1 Answer 1

1

Use Group By and MAX

insert into destination
select id, max(name), max(empid), max(company_id), max(addres) from staging
group by id

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

It will be time consuming there are 3 Min rows in staging

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.