Matching company names in two tables - sql

I have two tables with company names and their ids, the table Corporation_Name has ID and NAME, the other table DATA_Excel has CORPORATION as ID and C_Name as name; I have to match company names in the data table with Corporation Name to make sure all the companies exist if not i only have to insert the company which is not present in Corporation name.
Currently i am using this query:
Select Distinct (B.corporation), B.C_name
from data_excel B, corporation_name A
where B.C_name <> A.name
also some times this is the case:
87 Société Générale de Belgique
87 Societe Generale de Belgique

Your query is not finding what you are looking for. You are all companies from A and matching them with all companies in B, meaning it will return a record for Societe Generale <> Fortis
Start with this:
SELECT B.Corporation, B.C_Name
FROM data_excel B
LEFT OUTER JOIN corporation_name A
ON B.C_Name = A.Name
WHERE A.Name IS NULL
This will still not solve everything, you will still have to replace
ON B.C_Name = A.Name
With something like what John Doyle suggested, because Société will still not match Societe!

Related

Create a additional row from join sql?

I have 2 tables: Person and House with 1-n relation.
I want to the result return as picture below:
Row always have a Person column with a null House column.
Thanks.
you can use unionall to join the result set with person table something like
select p.name,h.name as housename from person p join house h on p.id=h.personid
union all (select name,null from person)
order by name,housename

Get Unique records from two hive tables

I have two hive table's. And both tables have one column column. There are some few rows which value is same in both tables
Now my requirement is join the two tables and exclude the common records from second table based on common column.
For example,
Table a:
Name. City. Country
Devid. Hyd. Ind
Steve. London. UK
John. Bangalore. Ind
Table B
Name. City. Country
Xxxx. Xxxxx. Ind
Yyyyy. Yyyy. US
Zzzz. Zzzzz. UK
Now my required output is
Name. City. Country
Devid. Hyd. Ind
Steve. London. UK
John. Bangalore. Ind
Yyyyy. Yyyy. US
I tried following logic
Select a.* From A a union
Select t.* From (
Select c.* From table B b right join table A c on
b.country = c.coubtry
Where b.id is null) t;
This query not completing, keep on running. Any workaround needed?
Please help me out guys.
Try the below code:
SELECT * FROM A
UNION
SELECT * FROM B
WHERE Country NOT IN
(SELECT DISTINCT Country FROM A)

SQL JOIN to get the compare two values/row from table "A" and the same row from table "B"

I have two tables with the following rows
Table A (transaction)
Order Seller Customer
1 300 500
Table B (Persons)
PersonID FullName
300 Peter White
500 Scott Bold
I want a result like this
Order Seller Customer FullName (Seller) FullName (customer)
1 300 500 Peter White Scott Bold
I've tried multiple things however which makes more sense is a join a table twice, however I'm getting:
Ambiguous column name
This is SQL Server 2019.
Basically I'm looking to retrieve info from the same table instead of creating additional tables. Is that possible? If yes, how do you do? Thank you in advance.
As #jarlh wrote in comment:
select t.order, t.seller, t.customer, sel.fullname, cust.fullname
from transaction t
join persons sel -- sel is an alias to persons table
on sel.personid = t.seller
join persons cust
on cust.personid = t.customer;
Query with join will return the result as long as both seller and customer exist in persons table -- here it should as source table names transactions :).
I have another form of query it still join table B twice.
This is archaic syntax which I don't recommend but for beginner know the concept of JOIN:
select t.*,B.FullName as FullName (customer) from
(
select A.Order,A.Seller,A.Customer,B.FullName as FullName(Seller)
from A,B where A.Seller=B.PersionID
) t, B where t.Customer=B.PersionID
The proper way of JOIN:
select t.*,B.FullName as FullName (customer) from
(
select A.Order,A.Seller,A.Customer,B.FullName as FullName(Seller)
from A JOIN B ON A.Seller=B.PersionID
) t JOIN B ON t.Customer=B.PersionID
Hoping this can help you.

Selecting full names of pairs of ISO-coded countries

Suppose I have got two tables, one of which is named A and contains two columns : country1 (as an ISO code), country2 (as an ISO code) whilst the other one is named B and contains two columns : code (ISO code of country) and name (full name of country).
I would like to list all pairs of countries that occur in table A using their full names, rather than their ISO codes.
If I had to get the full name of every country that occurs in the first column of table A, I would write te following:
SELECT name AS name1
FROM B
INNER JOIN A ON B.code = A.country1
However, I am struggling to find out how to get both columns with full names.
You should be able to join the code column from B on both the country1 and country2 columns from A. You just need to make sure you alias it differently on each join.
SELECT A.country1, A.country2, B1.name AS name1, B2.name as name2
FROM A
JOIN B AS B1 ON B1.code = A.country1
JOIN B AS B2 ON B2.code = A.country2
Join should always be on same column type from both tables. Isnull will work for you, If country codes are stored in different columns in TableA
SELECT name AS name1
FROM B
INNER JOIN A ON B.code = isnull(A.country1, a.Country2)
or Use case statement
SELECT name AS name1
FROM B
INNER JOIN A ON B.code = case when A.country1 is null then A.country2
else A.country1 end

SQL Insert into with select from multiple tables

I have the following 3 tables, Data_Excel contains the names, address, city and source of person; Person table has the name and ID; I need to insert into person_location the address source, address, city and ID...where ID comes from person table and name that exist against the id should be matched in the data_excel table to get all the details
Take a look at this very similar question which should provide the information you need to apply to your own problem.
The error is probably from this part of the query A.name, A.ID in (Select[...]
You can try..
INSERT INTO person_location
SELECT A.ID,A.P_name,source,P_address,P_city,P_country from data_excel de, person A where A.name = de.c_name;
If you need the ID > 6566 condition, you can add it at the end.
INSERT INTO person_location
SELECT A.ID,A.P_name,source,P_address,P_city,P_country from data_excel de, person A where A.name = de.c_name and ID > 6566;