merge statement - upsert - performing unique test in source table as well - sql

I need some help with SQL Server merge statement. I am using version 2008.
I have two tables table1 and table2 with 3 column in each table: name, age, lastname.
I want to do little variant of Upsert from table2 to table1. If record exists in table 1, ignore. If doesn't exist then insert.
I know following would work -
merge into [test].[dbo].[table1] a
using [test].[dbo].[table2] b
on a.name = b.name and a.lastname = b.lastname
when not matched then
insert (name, age, lastname) values (b.name, b.age, b.lastname)
I would like to know if I could do something like this? Currently following doesn't work:
merge into [test].[dbo].[table1] a
using [test].[dbo].[table2] b
on a.name = b.name and a.lastname = b.lastname
when not matched then
insert (select name, max(age), lastname from b group by name, lastname)
Basically I want to insert only 'unique records' from table 2 to table 1. Unique means name and lastname should be same.
Thanks.

Its not really an UPSERT operation its a simple insert and I would do something like this....
insert into [test].[dbo].[table1](name, age, lastname)
SELECT b.name, MAX(b.age) Age, b.lastname
FROM [test].[dbo].[table2] b
WHERE NOT EXISTS (SELECT 1
FROM [test].[dbo].[table1]
WHERE name = b.name
and lastname = b.lastname)
GROUP BY b.name, b.lastname
UPSERT would be if you updated records if they already existed.

For just insert you don't really Merge. An insert alone should be enough. But heres a way to do it
merge into [test].[dbo].[table1] a
using (
select
name,
lastname,
max(age) age
from [test].[dbo].[table2]
group by
name,
lastname
) b on
a.name = b.name and
a.lastname = b.lastname
when not matched
then
insert (
name,
lastname,
age
)
VALUES (
b.name,
b.lastname,
b.age
);

Related

How to execute SELECT statements in CASE

I have two SELECT queries let's say.
SELECT FullName FROM TableA
SELECT FirstName FROM TableB
I want to execute both statements in CASE or if there is any alternative for this.
If SELECT FullName FROM TableA returns NULL or no row at all, then it should run following query:
SELECT FirstName FROM TableB
I could use two CASE statements here, but there are around 20 columns in each table and that'll make 40 CASE statements eventually making query very lengthy.
SELECT FullName FROM TableA
union
SELECT FirstName FROM TableB
where not exists
(SELECT FullName FROM TableA)
Working Fiddle
I think this is what you are searching for:
IF EXISTS (SELECT 1 FROM TableA)
SELECT FullName AS Name, TableA.Field1 AS FinalField1,
TableA.Field2 AS FinalField2...
FROM TableA
ELSE
SELECT FirstName AS Name, TableB.Field1 AS FinalField1,
TableB.Field2 AS FinalField2...
FROM TableB

Sql table query for tables with no direct link

I have Table_A that has a column: 'FullName',columnB, .. and other columns
I have Table_B that has columns: 'FirstName', 'LastName','Job_OID'
Evidently, If we join the characters of FirstName and LastName, it matches Full_Name.
This is the only thing common between these two tables!
Problem: When the Full Name is "John Smith" in Table_A, I want to fetch his Job_OID from table_B.
In simple language,
select job_oid from table_B where ('FirstName' + 'LastName') = Table_A.FullName;
try this:
SELECT job_oid
FROM table_B
JOIN Table_A ON Table_A.FullName = Table_B.FirstName+' '+TableB.LastName
You can also write query in this way:
SELECT B.job_oid
FROM Table_A A
INNER JOIN table_B B ON A.FullName = CONCAT(B.FirstName, ' ', B.LastName)
I think, you must normalize your DB, but if it isn't possible, you can write following query:
SELECT B.*
FROM Table_B B
WHERE EXISTS
(SELECT 'PERSON'
FROM Table_A A
WHERE B.FirstName + ' ' + B.LastName = A.FullName)
You can use, otherwise, JOIN command like this:
SELECT B.*
FROM Table_B B
JOIN Table_A A
ON B.FirstName + ' ' + B.LastName = A.FullName
So, in this way you link two tables with a concatenation of two fields (FirstName and LastName of Table_B) with FullName stored in Table_A

SQL How to compare data in two tables and get the results that are different between two tables

There are 2 tables. Table a and b. A contains msisdn, firstname, secondname, lastname, regdate(registration data). Table b also has the same fields. I want to compare these two tables, the msisdn's, firstname and lastname fields. If msisdn X in table A has firstname as jim and lastname as halpert, and the same msisdn X has firstname as michael and secondname as scott in table B, i need to get these kinds of msisdn's as my query result. the one's with same msisdn in both tables and different names. if either of these names(first or last) mismatches, that should be shown as result.
I'm sorry if i did not explain the scenario accurately. I hope someone understands and answers this.
thanks :)
SELECT A.*, B.*
FROM TABLEA A
INNER JOIN TABLEB B ON A.MSISDN = B.MSIDN
WHERE A.firstname != B.firstname
OR A.lastname != B.Lastname
Select
*
From
Table a
join
Table2 b on a.msisdn = b.msisdn
where
(a.firstname != b.firstname) OR (a.lastname != b.lastname)
If your table is does not have any foreign key you can try this:
SELECT tableA.*, tableB.*
FROM tableA, tableB
WHERE tableA.col1 != tableB.col1
OR tableA.col2 != tableB.col2
You can change the operator with any operator you want,
Maybe it does not look so pro but it's easier to me :)

Getting the Emp_ID

In Table_A I have atrribute "Verantwortlicher". This attribute contains the last name and the first name of the person respectively i.e. "Doe John"
In Table_B I have two attributes "Lastname_NAM" and "Firstname_NAM" which of course contain lastnames and firstnames of a person respectively i.e.
"Lastname_NAM" - Doe
"Firstname_NAM" - John
Table_A has a foreign key (Responsible_Emp_ID) which is referencing Table_B´s primary key (Emp_ID)
What I would like is to join these two tables so that beside the strig-type full name in the Table_A I also have a ID of that person (which of course comes from Table_B)
I forgot to mention that I need this data so I can FILL the values in aforeign key. Table_A is EMPTY.
I´m using T-SQL
Please help,
Thanks, D.
Try following code
SELECT
B.EmpID,
ISNULL(A.Verantwortlicher,B.Firstname_NAM++B.Lastname_NAM)
FROM
TableB B
LEFT JOIN TableA A
ON B.EmpID = A.Responsible_Emp_ID
Try this:
--Populate TableA
INSERT INTO TableA(Responsible_Emp_ID, Verantwortlicher)
SELECT EmpID, FirstName + ' ' + LastName
FROM TableB B
--SELECT to join the 2 tables
SELECT B.EmpID, A.Verantwortlicher
FROM TableB B
INNER JOIN TableA A
ON B.EmpID = A.Responsible_Emp_ID
--Just make the join
SELECT C.FullName, A.EmpID FROM
(SELECT FirstName + ' ' + LastName AS FullName
FROM TABLE B) C
INNER JOIN TableA A
ON C.FullName = A.Verantwortlicher

SQL query get distinct records

I need help with a query to get distinct records from the table.
SELECT distinct cID, firstname, lastname,
typeId, email from tableA
typeId and email has different values in the table. I know this one causing to return 2 records because these values are different.
Is there anyway I can get 1 record for each cID irrespective of typeId and email?
If you don't care about what typeId and email get selected with each cID, following is one way to do it.
SELECT DISTINCT a.cID
, a.firstname
, a.lastname
, b.typeId
, b.email
FROM TableA a
INNER JOIN (
SELECT cID, MIN(typeID), MIN(email)
FROM TableA
GROUP BY
cID
) b ON b.cID = a.cID
If any one value for typeId and email are acceptable, then
SELECT cID, firstname, lastname,
max(typeId), max(email)
from tableA
group by cID, firstname, lastname,
should do it.
Is this what you are after:?
SELECT distinct a.cID, a.firstname,
a.lastname, (SELECT typeId from tableA
WHERE cID = a.cID), (Select email from
tableA WHERE cID = a.cID) from tableA
AS a