I need to join one column of a table to a column of another table.
Now these two column consists the geographic region data. But the issue is the column dont have exactly same strings od data.
For ex. Latin America in one column and LATM in another.
The data is table if had been same string would be the simplest joins but these two mean the same but then are different strings . What do I use to accomplish my task.
I need to do is
Select * from Table1 Inner Join Table2 on table1.region = table2.region
You would need to create a mapping table which maps every possible region in Table1.region to every possible region in Table2
for example your Mapping table be like.
MappingTable
--------------------------
Region1 | Region 2
--------------------------
Latin America | LATM
Europe | EUR
.....
The you can create a join like
Select *
from
Table1
inner join
MappingTable
on
Table1.region = MappingTable.Region1
inner join
Table2
on
MappingTable.Region2 = Table2.region
You need to make another table which contains information of TWO table Joining like 'Latin America' = 'LATM' and then have to use this table in join.
Related
I have two hive SQL tables which consist of following columns.
table_1
|customer_id | ip_address|
region_table
|country_name | region_name|
I tried,
SELECT table_1.customer_id, table_1.ip_address, getCountry(ip_address) AS Country, region_table.region_name FROM table_1 JOIN region_table ON region_table.country_name = Country;
getCountry() is UDF which returns the country name when the IP address
is passed into it. I want to use that country name to create another
column with the corresponding region from the region_table. And i want
to get the following table as my output.
customer_id | ip_address | Country | region_name
Any thoughts on what I'm missing in my query?
select c.customer_id
,c.ip_address
,getCountry(c.ip_address) as Country
,r.region_name
from table_1 c
join region_table r
on r.country_name =
getCountry(c.ip_address)
In case of Oracle, you can not refer column alias defined in SELECT statement in WHERE clause of same query!! Because database engine first evaluates WHERE clause and identifies eligible rows and then proceeds to fetch columns as defined in SELECT part of query.
In your case, right query should be
select
table_1.customer_id,
table_1.ip_address,
getCountry(ip_address) AS Country,
region_table.region_name
FROM table_1
JOIN region_table ON region_table.country_name = getCountry(table_1 .ip_address);
I have 3 columns in table A. I am trying to design a query that will call out all the values (in the three columns) that do not apepar in the 1 column I have in table B. If it helps to make it more clear, table B is a list of currencies in ISO codes and table A is three columns of currencies being used, I am identifying all those values that are NOT using ISO codes to denote their currency.
Currently, I can't seem to get them all to match to the one column, so I made 2 more columns in table B so I can match them individually. My constraints are, I cannot change table A and I must do this in one query. What I got so far is below.
SELECT m.Currency1, i.ISO_Code, m.Currency2 , i.ISO_Code1, m.Currency3, i.ISO_Code2
FROM A AS m
LEFT JOIN B AS i
ON m.Currency=i.ISO_Code
AND m.Currency2=i.ISO_Code1
AND m.Currency3=i.ISO_Code2
WHERE i.ISO_Code is NULL
OR i.ISO_Code1 is NULL
OR i.ISO_Code2 is NULL;
I wouldn't bother making multiple columns in 'B'. I played with this in SQLFiddle and got it to work.
Something like this:
SELECT
m.Currency1, i.ISO_Code,
m.Currency2, j.ISO_Code AS ISO_Code1,
m.Currency3, k.ISO_Code AS ISO_Code2
FROM A AS m
LEFT JOIN B as i
ON m.Currency1 = i.ISO_Code
LEFT JOIN B as j
ON m.Currency2 = j.ISO_Code
LEFT JOIN B as k
ON m.Currency3 = k.ISO_Code
WHERE
i.ISO_Code IS NULL OR
j.ISO_Code IS NULL OR
k.ISO_Code IS NULL
I have a little problem - since im not very experienced in SQL - about joining the same table on multiple values. Imagine there is table 1 (called Strings):
id value
1 value1
2 value2
and then there is table 2 (called Maps):
id name description
1 1 2
so name is reference into the Strings table, as is description. Without the second field referencing the Strings table it would be no problem, id just do an inner join on Strings.id = Maps.name. But now id like to obtain the actual string also for description. What would be the best approach for a SELECT that returns me both? Right now it looks like this:
SELECT Maps.id, Strings.value AS mapName FROM Maps INNER JOIN Strings ON Strings.id = Maps.name;
But that obviously only covers one of the localized names. Thank you in advance.
You can do this with two joins to the same table:
SELECT m.id, sname.value AS mapName, sdesc.value as description
FROM Maps m INNER JOIN
Strings sname
ON sname.id = m.name INNER JOIN
Strings desc
ON sdesc.id = m.description;
Note the use of table aliases to distinguish between the two tables.
As long as you want to get a single value from another table, you can use subqueries to do these lookups:
SELECT id,
(SELECT value FROM Strings WHERE id = Maps.name) AS name,
(SELECT value FROM Strings WHERE id = Maps.description) AS description
FROM Maps
I have two tables in a Microsoft Access 2007 database.
One of them has columns "name 1 & name 2" with 2000 records. The other table has more information: it has two columns named "code1 & code2" with 3000 records. Each name has a code means that every name has a code stored in the second table in the column code 1.
I want to make one table or query that shows me name 1 & name 2 with their specific code1 & code 2 from the second table .
And it must have only 2000 records
example :
tabel 1 :
name-1 name-2
-----------------------------
Abacavir Digoxin
Amprenavir Aspirin
tabel 2 :
code-1 drug1
----------------
xy1 Abacavir
xy2 Digoxin
yxr1 Amprenavir
uyv2 Aspirin
sample output :
name-1 code-1 name-2 code-2
-----------------------------------------
Abacavir xy1 Digoxin xy2
This table structure is poorly designed but I'll assume you are stuck with it. You have to create a query that joins Table2 twice... once for each of the relationships you are linking by. In my test I created an access db and named my tables simple Table1 and Table2 so you may need to alter this query slightly to match your table/column names.
SELECT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;
The fact you have 2K in the first and 3K in the second is a clue. Try adding the 'Distinct' key in your SQL statement:
SELECT DISTINCT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;
Otherwise I think Frank nailed it.
I need to update table one using data from table two. Table one and two are not related by any common column(s). Table three is related to table two.
Ex : table one(reg_det table)
reg_det_id | reg_id | results
101 | 11 | 344
table two :(temp table)
venue | results
Anheim convention center | 355
Table three (regmaster-tbl)
reg_id| venue
11 | Anaheim convention center
I need to update results column in table one using data from table two. But table one and two are not related. Table two and three and table one and three are related as you can see above. Can anyone please suggest any ideas! I need the results value to be 355 in table one and this data is coming from table 2, but these two are unrelated, and they can be related using table three. Sorry if it is confusing!
Fairly straight forward:
UPDATE T1
SET result = t2.results
FROM [table one] T1
INNER JOIN [table three] t3
on t1.reg_id = t3.reg_id
INNER JOIN [table two] T2
on t2.venue = t3.venue
Almost a question instead of an answer. :)
Couldn't you use an implied inner join?
UPDATE rd
SET rd.results = tt.results
FROM reg_det rd, regmaster rm, temptable tt
WHERE rm.reg_id = rd.reg_id
AND rm.venue = tt.venue;
I find it easier to read, and this syntax works in a SELECT statement, with the same meaning as an explicit inner join.
Try this:
UPDATE rd
SET rd.results = t.results
FROM reg_det rd
JOIN regmaster rm ON rm.reg_id = rd.reg_id
JOIN temptable t ON t.venue = rm.venue
WHERE t.results = 355
I added a WHERE clause because otherwise it will update all reg_det records that have matches in regmaster and temptable.