Getting the Emp_ID - sql

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

Related

How to find differences between 2 tables with different key names

So say I have 2 tables. TableA has keys FirstName, LastName, Age, Location. TableB has keys 1FirstName, 1LastName, 1Age, 1Location. Table A is a working table, and TableB is a reference table. How could I go about finding what records exist in TableB that DO NOT exist in TableA
Using an outer join will join all rows of left table to either right table or NULL
Then including only NULL from right hand side will give you what you need.
TableB as left side
SELECT
*
FROM (
SELECT
b.*,
a.*
FROM TableB b
LEFT OUTER JOIN TableA a
ON
b.1FirstName = a.Firstname
AND
b.1LastName = a.Lastname
AND
b.1Age = a.Age
AND
b.1Location = a.Location
) q
WHERE q.FirstName IS NULL

SQL SELECT and check column from second table

I have two tables in my database. From the Table B i collect some data. I include by using JOIN the data from Table A. In this table there is a column user_id. The user id in table B is the the id from Table A. No i want to get all data from table A but without showing data if the table A id is in the table B column user_id available. I think the trick is the correct usage of JOIN but i need to check the whole column and not just one line.
SQL Query (which is probably not working)
SELECT * FROM tableB
JOIN tableA
ON tableB.user_id = tableA.id
WHERE tableB.user_id != tableA.id
Please see my example i have prepared in Excel:
To select all the data from tableA where id is not available in tableB you don't need join rather you can use not exists or not in.
I would prefer not exists
Using not exists:
SELECT * from tableA a
WHERE NOT EXISTS (
SELECT 1
FROM tableB b
WHERE b.user_id = a.id
);
using not in:
SELECT * from tableA a
WHERE id not in (SELECT user_id FROM tableB );
DB-Fiddle:
create table TableA (id int, name varchar(50), lastname varchar(50));
insert into TableA values(1,'john','smith');
insert into TableA values(2,'Paul','smith');
create table TableB (id int, user_id int, something varchar(50));
insert into TableB values(1,2,'bla');
insert into TableB values(2,3,'bla');
Query: (using not exists)
SELECT * from tableA a
WHERE NOT EXISTS (
SELECT 1
FROM tableB b
WHERE b.user_id = a.id
);
Output:
id
name
lastname
1
john
smith
Query: (using not in)
SELECT * from tableA
WHERE id not in (SELECT user_id FROM tableB );
Output:
id
name
lastname
1
john
smith
db<fiddle here
I think I get what you want. You want not exists:
SELECT *
FROM tableB b JOIN
tableA a
ON b.user_id = a.id
WHERE NOT EXISTS (SELECT 1
FROM tableA a2
WHERE b.user_id = a2.id
);
EDIT:
I wrote the above, but I didn't fully follow the JOIN conditions. You either seem to want to join on id:
SELECT *
FROM tableB b JOIN
tableA a
ON b.id = a.id
WHERE NOT EXISTS (SELECT 1
FROM tableA a2
WHERE b.user_id = a2.id
);
Or no JOIN at all:
SELECT *
FROM tableB b
WHERE NOT EXISTS (SELECT 1
FROM tableA a2
WHERE b.user_id = a2.id
);

insert into with two selects and compare

It looks stupid in this example but here is what I want to do:
Table_a:
id fk_b_id full_name
1 [I want '10' here] [I want 'John, Doe' here]
Table_b:
id first_name
10 John
Table_c:
id full_name date
20 John, Doe 2020-01-01
I get all the full names that is not already in c and meets the criteria like this:
select distinct full_name
from Table_c
where full_name not in (
select full_name
from Table_a ) and date > GETDATE()
The result from the query should be inserted into Table_a together with Table_b's id (fk_b_id). So I need a compare between part of Table_a's full_name and Table_b's first_name to get the correct fk. I can do the compare like this:
where Table_b.first_name = LTRIM(RTRIM(RIGHT(Table_c.full_name, CHARINDEX(',', REVERSE(Table_c.full_name))-1 )))
Can I insert all the full names from table c (that meets the criteria) and the fk from table b (where their first names match) into table a in one query?
Sure, just use your condition to JOIN between tables B and C
INSERT INTO Table_a (fk_b_id, full_name)
SELECT DISTINCT b.id, c.full_name
FROM Table_c c
INNER JOIN Table_b b ON b.first_name = LTRIM(RTRIM(RIGHT(c.full_name, CHARINDEX(',', REVERSE(c.full_name))-1 )))
WHERE c.full_name not in (
select a.full_name
from Table_a a ) and c.date > GETDATE()
EDIT:
Also note, that if your table_a.full_name column can have NULL values, NOT IN sub-query will fail to give you expected results. I suggest rewriting it to use NOT EXISTS
WHERE NOT EXISTS
(SELECT * FROM Table_a WHERE Table_a.Full_Name = Table_c.FUll_Name )

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