Currently I have 3 tables like below
Master
ID_NUMBER
ZIPCODE
1
12341
2
12342
3
12343
4
12344
Table1
ID_NUMBER
CITYNAME
COUNTYNAME
1
NEW YORK
QUEENS
3
DETROIT
SUFFOLK
Table2
ID_NUMBER
CITYNAME
COUNTYNAME
2
ATLANTA
ROCKLAND
4
BOSTON
WINCHESTER
My desired output is like below. I want to filter based on the zipcode from master table
ID_NUMBER
ZIPCODE
CITYNAME
COUNTYNAME
2
12342
ATLANTA
ROCKLAND
How would i go about writing a query for this? Below is what i have tried but it's giving me null values if the ID_NUMBER is not found on that particular table.
SELECT mstr.id_number,
mstr.zipcode,
t1.cityname,
t1.countyname,
t2.cityname,
t2.countyname
FROM MASTER mstr
LEFT JOIN Table1 t1 ON mstr.id_number=t1.id_number
LEFT JOIN Table2 t2 ON mstr.id_number=t2.id_number
WHERE mstr.zipcode='12342'
Use COALESCE():
SELECT mstr.id_number,
mstr.zipcode,
COALESCE(t1.cityname, t2.cityname) as cityname
COALESCE(t1.countyname, t2.countyname) as countyname
FROM MASTER mstr LEFT JOIN
Table1 t1
ON mstr.id_number = t1.id_number LEFT JOIN
Table2 t2
ON mstr.id_number = t2.id_number
WHERE mstr.zipcode = '12342
'
Another approach you can try since your tables are identical is to join master with the union of the tables
with t as (
select Id_number, Cityname, Countyname
from t1
union all
select Id_number, Cityname, Countyname
from t2
)
select *
from Master m join t on m.Id_number=t.Id_Number
where m.zipcode='12342'
Related
I am working with two tables:
Table1
Name Team ID
Robinho Brazil 1
Ronaldo Brazil 2
Totti Italy 3
Baggio Italy 4
Rooney England 5
Table2
ID Football_Club Address
1 Chelsea London
3 Fulham London
I would like a new table with all columns included but to only include the two rows where the two tables intersect. I am using the following SQL Query which is very wrong:
SELECT id,
NAME,
team
FROM table1
INTERSECT
SELECT id,
football_club,
address
FROM table2
How can I rewrite this to bring about the correct result using INTERSECT?
You want an INNER JOIN rather than an INTERSECT. Replace * with the columns you need using the table aliases.
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id;
Master table
SerNo HospitalId CityId
1 1 1
2 1 1
3 2 2
4 3 2
5 1 1
HospitalMaster
HospitalId HospitalName
1 ABC
2 XYZ
CityMaster
CityId City
1 Delhi
2 Bombay
Result
I need something like this
City TotalHospital
Delhi 1
Bombay 2
I tried joining the tables but I keep getting the total rows of the columns and not of the hospitals.
Thank you.
Left join the city master table to a subquery which finds the hospital counts for each city. Note carefully that we only count distinct hospitals, because a hospital city relationship may appear more than once in the master table.
SELECT t1.City, COALESCE(t2.cnt, 0) AS TotalHospital
FROM CityMaster t1
LEFT JOIN
(
SELECT CityId, COUNT(DISTINCT HospitalId) cnt
FROM Master
GROUP BY CityID
) t2
ON t1.CityId = t2.CityId;
Demo
Try this:
SELECT C.City,COUNT(DISTINCT HospitalID)TotalHospital
FROM CityMaster C
JOIN Master_table M ON M.CityId=C.CityId
GROUP BY C.City
You could apply join
select M.City,count(distinct M.HospitalId) from CityMaster C inner join Master M ON C.CityId = M.CityId
group by M.City
You can do it with using JOIN
Just replace #city, #hospital, #table with your table names.
select C.City,T.CityId from #city C,#hosp H,#table T WHERE T.CityId = C.CityId AND T.HospitalId = H.HospitalId Group by C.City,T.CityId
As we need city name along with count, we can get by join city master and master tables.
select max(C.cityname), count(distinct M.HospitalId)
from CityMaster C
inner join Master M
on C.Cityid = M.CityId
group by M.cityid
I have a data row with multiple fk pointing to the same table. How can I generate this:
table 1
id | name | fk1 | fk2 | fk3
1 test EA US NULL
2 test2 Null UK US
table 2
id | details
EA East Asia
US United States
UK United Kingdom
I want to generate something like this
id | name | details
1 test East Asia
1 test United States
2 test2 United Kingdom
2 test2 United States
I've been looking around but probably I'm typing the wrong search keyword or phrase.
thanks
this is what I did
select t1.id,t1.name,t2.details from table1 t1
left join table2 t2 on t2.id = t1.fk1
union
(select t1.id,t1.name,t2.details from table1 t1
left join table2 t2 on t2.id = t1.fk2
)
union
select t1.id,t1.name,t2.details from table1 t1
left join table2 t2 on t2.id = t1.fk3
but this table generates row with null
Use UNPIVOT to get each of your fk columns as a separate row.
select u.id, u.name, t2.details
from table1 t1
unpivot(
region for regions in (fk1, fk2, fk3)
) u
join table2 t2 on t2.id = u.region
in my database i have 2 tables.
table1
i have ID and NAMES
table2
i have ID, IDASSOCIATION, QUANTITY
so
i have 2 names in table1:
john and tom
and in table2 i have 3 lignes
john, 1
tom, 1
john, 1
nombre one is the quantity
in my result i want get
john = 2
and tom = 1
so i do this:
sql = "SELECT t1.*, t2.IDASSOCIATION, (SELECT SUM(t2.id_qte) FROM associationdepotarticle t2 WHERE t1.fusiontable = t2.fusiontable GROUP BY t2.IDASSOCIATION) as id_qte FROM articletable t1, associationdepotarticle t2";
but i not get this:
john = 2
tom = 1
why ? what i will do, i need correction please
You can just join the tables together and use sum:
select t1.name, sum(t2.quantity)
from table1 t1
join table2 t2 on t1.id = t2.idassociation
group by t1.name
It's not completely clear from your sample data what to join on, but I assume it's the idassociation field. If you want to return those names in table1 which aren't in table2, then use an outer join.
I have following senario
table1 - having 1 record with 1001(primary key)
table2 - having 3 record with same id (1001) - not as primary key
table3 - having 3 record with same id (1001) - not as primary key
The join of the first 2 tables is returning 3 rows (it is fine). But, if I join table3 then it is returning 9 rows. I know how join work and result is expected.
I need only 3 rows in result. something like shown below
id name age sex city
1001 Jhon 20 A Z
1001 Jhon 20 B Y
1001 Jhon 20 C X
Here is fiddle example
This query may do what you ask for. To change the combination between Table2 and Table3 you can work on the two ORDER BY clauses. Really strange requierement anyway! Are you sure you're doing it right?
with ord_t2 as (
select idt1 as id, sex, row_number() over(partition by idt1 order by sex) as ord_no
from table2 t2
), ord_t3 as (
select idt3 as id, city, row_number() over(partition by idt3 order by city) as ord_no
from table3 t3
), t2_x_t3 as (
select id, sex, city
from ord_t2
natural full outer join ord_t3
)
select *
from Table1
natural left join t2_x_t3