Getting a value from another table for multiple columns - sql

I have a table City with two fields, CityCode and RegionCode
I have another table Code with two fields Code and CodeMeaning
I want to write a select statement that will display both cityCode, regionCode and their meanings for each city.
If i had to get one meaning from Code table i could do it with join but i don't know how to get values for both columns.
City Table Data
------------------------
CityCode RegionCode
34 53
41 43
Code Table Data
-----------------
Code Meaning
34 New York
41 Boston
53 North
43 South
Desired Output
------------------
CityCode RegionCode Region City
34 53 North New York
41 43 South Boston

Use two joins:
select cc.meaning as city, cr.meaning as region
from city c left join
code cc
on c.citycode = cc.code left join
code cr
on c.regioncode = cr.code

this is a bit poor db design but you can get the data with 2 joins to the codes table:
select c.*, c1.data as city, c2.data as region
from city_table c
join code_table c1 on c1.code = c.city_code
join code_table c2 on c2.code = c.region_code

Related

How to apply different conditions to join, bases a columns value in DB2

Am having a data as below
Table emp
Cty
name
flag
New York
aa
na
Gua
bb
city
Table city
Id
city
name
1
new york
aa
2
ohio
bb
I want to apply join based on flag columns value in single query.
Such as when flag is NA
flag='na' then emp.name=city.name
flag='City' then emp.name=city.name and emp.cty=city.city
Try this:
SELECT *
FROM EMP
JOIN CITY ON emp.name=city.name
and (emp.flag='na' or emp.flag='city' and emp.cty=city.city)

How to replace multiple columns in one SQL query?

I have 2 tables: country and trip.
A trip can have up to 3 country codes.
country table
country_code
country_name
FRA
FRANCE
IRL
IRELAND
JPN
JAPAN
MAR
MOROCCO
NZL
NEW ZEALAND
trip table
trip_id
country_code
country_code2
country_code3
1
FRA
IRL
JPN
2
MAR
NZL
My goal is to have country names displayed on the trip table instead of country codes.
I succeed to have only 1 country code replaced, thanks to the left join clause. I would like to have up to 3 country names displayed per row.
SELECT trip_id, country_name
FROM trip
LEFT JOIN country ON country_code = country_name
The actual output of the trip table:
trip_id
country_name
1
FRANCE
2
MOROCCO
Is there a way to replace each country code with its corresponding country name?
The EXPECTED output of the query from the trip table:
trip_id
country_name
country_name2
country_name3
1
FRANCE
IRELAND
JAPAN
2
MOROCCO
NEW ZEALAND
Thank you!
You could add two more joins
SELECT trip_id, c1.country_name, c2.country_name, c3.country_name
FROM trip t
left join
country c1
on t.country_code = c1.country_code
left join
country c2
on t.country_code2 = c2.country_code
left join
country c3
on t.country_code3 = c3.country_code
The cleanest way of accomplishing this query is using subqueries:
SELECT t.trip_id,
(SELECT country_name FROM country WHERE country_code = t.country_code) "c1",
(SELECT country_name FROM country WHERE country_code = t.countty_code2) "c2",
(SELECT country_name FROM country WHERE country_code = t.country_code3) "c3"
FROM trip t

Sql join in two tables and return empty tab as Unavaliable

In a SQL join, table 1 contains person info with city and table 2 contains city matched to country such as:-
Table #1
ID Name City
-------------------------
1 Kishan Pokhara
2 Ram Delhi
3 Shyam Beijing
Table #2
City Country
----------------------
Pokhara Nepal
Delhi India
I want to get the person ID, Name, Country so while joining the tables I want these items and if there is no country available for a city, I want "Unavailable" written in the country columns. Thanks
Try the below using left join and use coalesce() function to replace null country as 'Unavailable'
select id, name, a.city,coalesce(country,'Unavailable') as country
from table1 a left join table2 b on a.city=b.city

Moving rows with multiple return data to columns to ensure one row from results in SQL Server

I currently have a join that can results in multiple rows. Instead of using a join to get return results I would like to only return one but a particular column from the join table to be listed out in columns to show each one with only the one row.
select u.city, u.state, u.county, u.zip, c.local_code
from usa u
left join code c where c.zip = u.zip
where zip = '90210'
Sample result
city state county zip local_code
----------------------------------------------------
salt lake utah lake county 90210 12A
salt pond utah lake county 90210 12C
sea salt utah lake county 90210 12B
Since there are multiple cities for that one zip, I would like to split that up into separate columns instead and name them local_code_1 - local_code_6 to fill any potential codes that come through in one row and remove the city name.
So I would like results like:
state county zip local_code_1 local_code_2 local_code_3 local_code_4 local_code_5 local_code_6
utah lake county 90210 12A 12C 12B
This code needs some work to complete your preferences, but I am thinking something like this might help you out. It's multiple CTE's combined, each of them joined by another rownumber.
;WITH CTE AS
(
SELECT U.CITY, U.STATE, U.COUNTY, U.ZIP, C.LOCAL_CODE
FROM USA AS U
LEFT JOIN CODE C WHERE C.ZIP = U.ZIP
WHERE ZIP= '90210'
)
SELECT *
FROM CTE AS C
LEFT JOIN CTE_JoinedTable AS C2 ON C.ID = C2.ID AND C2.RowNumb = 2
LEFT JOIN CTE_JoinedTable AS C3 ON C.ID = C3.ID AND C3.RowNumb = 3
LEFT JOIN CTE_JoinedTable AS C4 ON C.ID = C4.ID AND C3.RowNumb = 4
LEFT JOIN CTE_JoinedTable AS C5 ON C.ID = C5.ID AND C3.RowNumb = 5
LEFT JOIN CTE_JoinedTable AS C6 ON C.ID = C6.ID AND C3.RowNumb = 6
WHERE C.RowNumb = 1
As already hinted by Andrea above, you are searching for a Pivot construction. You could assign a sequence nr by state, county and zip and then Pivot on this value.
DECLARE #t TABLE([city] VARCHAR(255),[state] VARCHAR(255),[county] VARCHAR(255),[zip] VARCHAR(25),[local_code] VARCHAR(25));
INSERT INTO #t VALUES
('salt lake','utah','lake county','90210','12A')
,('salt pond','utah','lake county','90210','12C')
,('sea salt','utah','lake county','90210','12B');
SELECT pvt.*
FROM (
SELECT [state]
,[county]
,[zip]
,[local_code]
,[seq]=ROW_NUMBER() OVER(PARTITION BY [state],[county],[zip] ORDER BY [local_code] ASC)
FROM #t
) src PIVOT(MAX(local_code) FOR seq IN([1],[2],[3],[4],[5],[6])) AS pvt;
Yielding
state county zip 1 2 3 4 5 6
--------------- --------------- ---------- ----- ----- ----- ----- ----- -----
utah lake county 90210 12A 12B 12C NULL NULL NULL
you can use pivot
select state,country,zip,[1] as local_code1,
[2] as local_code2,
[3] as local_code3,
[4] as local_code4,
[5] as local_code5,
[6] as local_code6
from
(
select state,country,zip,local_code,
rn= row_number() over (partition by state,country,zip order by local_code)
from t
) sr PIVOT (max(local_code) for rn in ([1],[2],[3],[4],[5],[6]) ) as pvt
http://sqlfiddle.com/#!18/358ae/40
state country zip local_code1 local_code2 local_code3 local_code4 local_code5 local_code6
utah lake county 90210 12A 12B 12C (null) (null) (null)

Select Statement to return TaskID if column Exists in another table

I have two tables.
First table is called task the second table is named countries.
My task table
ID TaskID Country CustomerID
------------------------------------------
1 213 china 22
2 213 USA 24
3 213 china 26
4 214 Canada 28
Countries table
ID Country
---------------
1 USA
2 Japan
3 England
I need a select statement that returns all task ID's that doesnt have its country i the countries table.
In this case I would need to return TASKID: 214 because canada is not in the countries table. I would not get TASKID: 213 because USA is in the countries table.
Try this:
SELECT
TaskID
FROM
Task T LEFT OUTER JOIN Countries C ON (T.COUNTRY = C.COUNTRY)
GROUP BY
TaskID
HAVING
COUNT(C.ID) = 0
Try this,
select taskid
from task where taskid not in
(select taskid from task where country in
(select country from countries))
Try like this,
SELECT t.taskid
FROM task t
WHERE EXISTS (
SELECT 1
FROM countries
WHERE country = t.country
)