Sql table query for tables with no direct link - sql

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

Related

Add two string columns from different tables

I am trying to add two columns together of char data type in order to combine their code with description.
table1 has all of the data I need and I am only referencing table2 in order to get the description.
For example, table1 has column:
Code
1
2
and table 2 has columns:
Code Description
1 Football
2 Soccer
I'm basically trying to write a query where I can have one column show
1 - Football
2 - Soccer
I have tried:
SELECT
a.Code + ' - ' + b.Description
FROM table1 a
LEFT JOIN table2 b
ON a.Code = b.Code
and while this does add the columns together, it also generates a lot of duplicate rows for some reason, I am assuming because of the LEFT JOIN.
Basically I am just wanting that whatever code is in table1, to match that code in table2 and to bring over that Description.
With the Left Join you get the values in Table A that may not exist in Table B. To eliminate the duplicates you could try
SELECT DISTINCT
a.Code + ' - ' + b.Description
FROM table1 a
LEFT JOIN table2 b
ON a.Code = b.Code
OR
SELECT
a.Code + ' - ' + b.Description
FROM table1 a
LEFT JOIN table2 b
ON a.Code = b.Code
GROUP BY a.Code, b.Description
You could use ISNULL to handle missing value from table2:
SELECT
a.Code + ISNULL(' - ' + b.Description, '')
FROM table1 a
LEFT JOIN table2 b
ON a.Code = b.Code
use coalesce in case of null for 2nd table and use distinct incase of avoid duplicate
SELECT distinct a.Code +' - ' + coalesce(b.Description,'')
FROM table1 a
LEFT JOIN table2 b
ON a.Code = b.Code
or try like below and i dont think you needed join you can do it just by using 2nd tabale
SELECT
b.Code ||' - ' || b.Description)
from
table2 b
What is the relationship between table1 and table2? The only way I see the above query returning duplicates is: If relationship between table1 and table2 is 1:Many. Meaning one row in table1 can match to multiple rows in table2.
Do you need to fetch a code that exists in table1 even if it does not have a corresponding description in table2? If so, use a LEFT JOIN like below.
If table2 has direct duplicates, then using a DISTINCT will remove
duplicates in the final result:
SELECT DISTINCT
a.Code + ' - ' + COALESCE(b.Description, '')
FROM table1 a
LEFT JOIN table2 b
ON a.Code = b.Code;
If one code has multiple different descriptions in table2, then your
final resultset is expected to have multiple rows for the same code
but with different description by running the above query because of
the nature of data in the tables.
Do you need to fetch a code that exists in table1 only if it has a corresponding description in table2? If so, use a INNER JOIN like below.
SELECT DISTINCT
a.Code + ' - ' + b.Description
FROM table1 a
INNER JOIN table2 b
ON a.Code = b.Code;

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 :)

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

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
);

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 Server: use left join to select a column from two tables as a single column

I am trying to build a select query that will essentially left join two tables and display one column from each table as a single column.
the table structure is similar to:
table a:
id, email
table b:
id, tablea_id, email
I am trying to get a single column of email and email (with no dupes or nulls ideally).
ideal results would be:
one#one.com
two#two.com
three#three.com
and the email address that is returned could be from either a or b.
Maybe a union is what would work best, but I not able to figure out how to do a union on the second table based on the id of the first table.
When searching for a solution, perhaps my wording is bad, but I can't find any examples.
thanks for any help.
It seems that you want something on the lines of this:
SELECT email
FROM TableA
UNION
SELECT B.email
FROM TableA A
JOIN TableB B
ON A.id = B.TableA_id
If you simply want a unique list of email addresses from either table you can do:
Select email
From TableA
Union
Select email
From TableB
If you are looking for a unique list of email addresses from Table B and those from Table A that exist in Table B, then you can do:
Select TableA.email
From TableA
Join TableB
On TableB.TableA_id = TableA.id
Union
Select email
From TableB
If, per your comments, you need all rows from Table A and only rows from Table B where they exist in Table A:
Select email
From Table A
Union
Select TableB.email
From TableB
Join TableA
On TableA.id = TableB.TableA_id
How about this:
SELECT DISTINCT a.email+ ' ' + b.email FROM tableA a LEFT JOIN tableB b on a.Id = b.tablea_Id WHERE b.email IS NOT NULL
This could help:
SELECT
id, email
FROM
a
UNION
(
SELECT
a.id AS id, b.email AS email
FROM
b
INNER JOIN
a
ON
a.id = b.tablea_id
) b