I have 3 tables as follows in SQL:
Accounts, containing
id zip city
---- ----- ---------------
121 20085 Los Angeles
Customer, containing
user id zip
------- ------
121 20085
Addr, containing
zip city-state
----- ----------------
121 Los Angeles, CA
I want to add an extra column in Accounts called location which will be a concatenated field that will take city from Accounts concatenate it with city-state from Addr by matching the city.
I cannot do the regular join as all the zip in Accounts are not there in Addr so I will loose many records, but the table Customer contains all the zip and user id so I think I can make use of that.
The output desired:
Accounts, containing
user id zip city location
------- ----- ---------------- ----------------
121 20085 Los Angeles Los Angeles;Los Angeles, CA
Are you after something like this:
SELECT
`c`.`user_id`,
`c`.`zip`,
`a`.`city`,
CONCAT_WS( ';', `c`.`city`, `aa`.`city-state` ) AS `location`
FROM
`Accounts` AS `a`
LEFT JOIN
`Addr` AS `aa`
ON
`aa`.`zip`, `a`.`id`
LEFT JOIN
`Customer` AS `c`
ON
`c`.`user id`, `a`.`id`
Related
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)
I have the following tables:
Table Offices:
OfficeID MainAddId SubAddId1 SubAddId2 PortAddId1
------- -------- -------- -------- --------
2 1 2 3 5
Table Address:
AddressID Street City ZipCode State
------- -------- -------- -------- --------
1 Forest Ave New York 10001 New York
2 Morris St Philadelphia 19019 Pennsylvania
3 David St Raleigh 27513 North Carolina
Table Port:
PortID PortName Street City ZipCode State
------- ------- -------- -------- -------- --------
5 New York Harbour Bay St New York 10001 New York
I want to write an SQL request such that if any of the addresses Id in the office table is not null,
it will return the list of addresses in a list like:
AddressID Street City ZipCode State
------- -------- -------- -------- --------
1 Forest Ave New York 10001 New York
2 Morris St Philadelphia 19019 Pennsylvania
3 David St Raleigh 27513 North Carolina
5 Bay St New York 10001 New York
Any help on how I can do this please? Thanks
Here is what I tried (partly cause it's not working):
select *
from Office offi
left join Address add1 on offi.MainAddId = add1.AddressID
left join Address add2 on offi.SubAddId1= add2.AddressID
where offi.OfficeID = 2;
However, this is returning the addresses on a single line.
Don't use multiple joins if you really need them. Instead of that, you can use OR after ON in join.
I solved using UNION:
select a.AddressID, a.street, a.city, a.zipcode, a.state
from offices o
inner join address a on (o.MainAddid = a.addressId or o.subaddid1 = a.addressid or o.subaddid2 = a.addressid)
union
select p.PortId, p.street, p.city, p.zipcode, p.state
from offices o
inner join port p on p.portId = o.PortAddId1
Check demo on DB<>FIDDLE
Try this one query :
SELECT *
FROM ( SELECT AddressID, Street, City, ZipCode, State
FROM address
UNION
SELECT PortID, Street, City, ZipCode, State
FROM Port) w
WHERE addressid IN ( SELECT Addresses
FROM ( SELECT OfficeID, MainAddId, SubAddId1, SubAddId2, PortAddId1
FROM Offices) p
UNPIVOT ( Addresses
FOR Offices IN (MainAddId, SubAddId1, SubAddId2, PortAddId1)) AS unpvt );
I have 3 table:
nation (name PRIMARY KEY);
city (name PRIMARY KEY, nation REFERENCES nation(name))
overflight (number, city, PRIMARY KEY (number, city))
The overflight table content is something like below:
AA11 city1
AA11 city2
BB22 city1
BB22 city3
etc.
I need to select only overflight that doesn't have city from a certain nation in the city field.
I've tried with:
SELECT number
FROM overflight
JOIN city ON overflight.city = city.name
WHERE overflight.city NOT IN (
SELECT name FROM city WHERE nation = some_nation
)
GROUP BY number;
but it doesn't work because it doesn't list the row of overflight that have city from some_nation but can happen that the same overflight have another row in the table that doesn't have city in some_nation. How can I display only the overflight that doesn't have city in some_nation at all?
Hope that I've explained my problem as clear as possible.
EDIT
This is exact content of overflight table:
AZ 7255 Rome
AZ 7255 Milan
AZ 608 Rome
AZ 608 New York
AA 1 New York
AA 1 Los Angeles
BA 2430 New York
BA 2430 Los Angeles
Suppose that I want to show the overflight that doesn't fly over any city in Italy. I need that the result is like this
AA 1 New York
AA 1 Los Angeles
BB 2430 New York
BB 2430 Los Angeles
Join the tables to get the overflight numbers that do have a city from the nation that you want to exclude and use the operator NOT IN to select all the other oveflights:
SELECT * FROM overflight
WHERE number NOT IN (
SELECT o.number
FROM overflight o INNER JOIN city c
ON o.city = c.name
WHERE c.nation = 'Italy'
)
See the demo.
Results:
number
city
AA 1
New York
AA 1
Los Angeles
BA 2430
New York
BA 2430
Los Angeles
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
I have records as follow in a tables on sql server 2005
fname lname address zip
xxx yyy UK 001
zzz yyy UK 001
aaa yyy UK 002
ddd jjj US 003
eee jjj US 003
I need to get the result in the following format
fname lname address zip
xxx,zzz yyy UK 001
ddd,eee jjj US 003
Basically every records which have a count address and zip 2 times will have their first name grouped and separated by comma.
Ok Here is my approach: but not working and stuck right now
select fname, lname, address, zip from table people
where address is not null
and zip is not null
group by address,zip
having count(address)=2 and count (zip)=2
order by address
-- Now to coalesce the records I am using
SELECT fname = COALESCE(fname + ', ', '') + ISNULL(fname, 'N/A'), fname, lname,streetname, housenumber
FROM people
WHERE address is not null and zip is not null
group by address,zip
having count(address)=2 and count (zip)=2
order by address
I don't think this is a duplicate because it doesn't require anything like group_concat(). The OP is specifically asking for two times, and you can get that like this:
select min(fname) + ',' + max(fname), lname, address, zip
from table t
group by lname, address, zip
having count(*) = 2;
Of course, a general answer with more matching rows can't be solved this way, but the question specifically says "zip 2 times".