Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to join table 1 and table 2 by counting the people in table two who visited the combo in table 1, so the result would look like
table 1
City1 City2
Rome Berlin
Moscow Berlin
....many more rows (to be clear- this was just an excerpt)
table 2
Person City
Peter Rome
Peter Paris
Peter Berlin
Peter Moscow
Paul Rome
Paul Moscow
Mary London
Mary Moscow
Mary Berlin
... many more rows
What I want it to look like:
Rome Berlin 0
Moscow Berlin 3
London Berlin 1
Any help appreciated.
Here is what I have done so far:
select a.city1
, a.city2
, count(case when city=a.city1 and city=a.city2 THEN city ELSE NULL END)
from table1 a join table2 b
One way you can do this is by joining to each of the cities and then aggregating, counting up when there is a match in both tables. This form of the query assumes there are no duplicates in the second table:
select t1.city1, t2.city2,
sum(case when t21.person is not null and t22.person is not null then 1 else 0 end) as cnt
from table1 t1 left outer join
table2 t21
on t1.city1 = t21.city left outer join
table2 t22
on t1.city1 = t22.city and t22.person = t21.person
group by t1.city1, t2.city2;
EDIT:
You can also do this with two levels of aggregation and one join. This is pretty easy to explain. The inner query counts the number of cities that a person matches for each pair of cities. The cnt is 0, 1, or 2. The outer query then counts the number where cnt = 2, because these are the ones that match both cities.
select city1, city2, sum(case when cnt = 2 then 1 else 0 end) as cnt
from (select t1.city1, t2.city2, person, count(t2.person) as cnt
from table1 t1 left outer join
table2 t2
on t2.city in (t1.city1, t1.city2);
The advantage of this approach is that adding new cities is easy, as well as getting more information, such as how many people match exactly one of the cities.
Sample data is incorrect, no London-Berlin combination in table1 and result should be
Rome Berlin 1
Moscow Berlin 2
London Berlin 1
However it can be achieved: you want the combinations of the cities of table2 so join table2 to itself based on person.
You also want to mach the combinations in table1 so join table1 to the previous result.
You need a counter so group on the last resultset:
select t2.city, t3.city, count(t1.city1) as counter
from table2 as t2
inner join table2 as t3 on t2.person = t3.person and t2.city <> t3.city
inner join table1 as t1 on t1.city1 = t2.city and t1.city2 = t3.city
group by t2.city, t3.city
The result will be as requested but you have to enter correct data in table1, otherwise you can get wrong result.
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;
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'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have two table with different column name and most of the values are same . I want the matched and unmatched data from the other table.
***
src-table
--------------------------------------------------------------------------------
eid | ename | email | country
--------------------------------------------------------------------------------
1 as as#gmail.com india
2 bs bs#gmail.com usa
3 cs cs#gmail.com usa
4 ds ds#gmail.com india
--------------------------------------------------------------------------------
tgt_table
--------------------------------------------------------------------------------
eid | ename | email | country
--------------------------------------------------------------------------------
1 as as#gmail.com india
2 bs b#gmail.com india
3 cs cs#gmail.com usa
4 ds d#gmail.com india
--------------------------------------------------------------------------------
expected output for matched_data
--------------------------------------------------------------------------------
src_coloumnname | src_data | tgt_colomnname | tgt_data
--------------------------------------------------------------------------------
eid 1 eid 1
eid 2 eid 2
eid 3 eid 3
eid 4 eid 4
ename as ename as
ename bs ename bs
ename cs ename cs
ename ds ename ds
email as#gmail.com email as#gmail.com
email cs#gmail.com email cs#gmail.com
country india country india
country usa country usa
country india country india
----------------------------------------------------------------------------
***
similarly unmatched records
how can i achieve that? can some one plz help me?
It could be done using one column at a time:
SELECT 'eid' AS match_column, l.eid AS src_value, r.eid AS tgt_value, CASE WHEN l.eid = r.eid THEN 'match' ELSE 'no match' END AS result
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
UNION ALL
SELECT 'ename', l.ename, r.ename, CASE WHEN l.ename = r.ename THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
UNION ALL
SELECT 'email', l.email, r.email, CASE WHEN l.email = r.email THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
UNION ALL
SELECT 'country', l.country, r.country, CASE WHEN l.country = r.country THEN 'match' ELSE 'no match' END
FROM table1 AS l
INNER JOIN table2 AS r ON l.eid = r.eid
It matches columns where the rows match. It does not check rows that are missing from either table.
you can do a simple join, i think you can do this
SELECT * FROM T1 FULL OUTER JOIN T2 ON TRUE
or
SELECT * FROM T1 , T2
for everyting
SELECT * FROM T1 FULL OUTER JOIN T2 ON T1.C1 <> T2.T1_C1
for the unmatched
SELECT * FROM T1 FULL OUTER JOIN T2 ON T1.C1 = T2.T1_C1
for matched
you can also change type of join for matching
here the join documentation
https://www.w3schools.com/sql/sql_join.asp
you can also
SELECT T1.C1, T2.C1,T1.C2, ..., CASE WHEN T1.C1=T2.T1_C1 THEN 'MATCH' ELSE 'NOT MATCH' END FROM T1,T2
for have you result with the evidence of matching
this is the documenation of case
https://www.w3schools.com/sql/sql_case.asp
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.
Sorry for repost but this will help you understand the scnario better-
For each member, there can be two types of addresses (mail and legal- based on two diff indicators). My goal is extract both the adress and show them in one column for each member id.
TABLE1
Address_key(PK) Country City PostCode
1 UK London 1111
2 US New York 2222
3 Spain Madrid 3333
4 France Paris 4444
5 Swiss Munich 5555
Table 2
Member Key(PK) Memebr ID
1 1
2 2
3 3
4 2
Table3
Address Key Member Key Mail Ind Legal Ind
1 1 Y N
2 1 N Y
3 2 Y Y
4 4 N Y
5 4 Y N
My goal is to get the mail address(based on mail ind) and legal adress(based on legal ind) for each member id.
SO my output should be -
Member Key Member ID Country City Postcode Legal Country Legal City Legal Postcode
1 1 UK London 1111 US New York 2222
2 2 Spain Madrid 3333 Spain Madrid 3333
4 2 Swiss Munich 5555 France Paris 4444
Can anyone help how to achieve this ? I am using oracle 10m toad 9.0
Which is better to use: Inner query or simple join ?
Something like the below? With your column naming i'm a little confused with Table2 Member Key(PK) , Memebr ID and what their relationship is to Table 3 Member ID. My best guess is below:
Select [Member Key], t2.[Member Id], t1.*
FROM TABLE2 t2
INNER JOIN TABLE3 t3 on t3.[Member Key] = t2.[Member Id]
INNER JOIN TABLE1 t1 on t1.[Address Key] = t3.[Address Key]
Since you have two types of addresses, you could join twice to TABLE3, once for the Mailing address and next for the legal address.
select T2.Member_Key, T2.Member_id
,coalesce(T1A.Country,''), coalesce(T1A.City,''), coalesce(T1A.PostCode,'')
,coalesce(T1B.Country,''), coalesce(T1B.City,''), coalesce(T1B.PostCode,'')
from Table2 T2
left join Table3 T3A on T2.Member_Key=T3A.Member_Key and T3A.Mail_Ind='Y'
left join Table1 T1A on T3A.Address_key = T1A.Address_Key
left join Table3 T3B on T2.Member_Key=T3B.Member_Key and T3B.Legal_Ind='Y'
left join Table1 T1B on T3B.Address_key = T1B.Address_Key
Another way would be to join once and use a CASE expression, thus:
select T2.Member_Key, T2.Member_id
,max(coalesce(CASE T3.Mail_Ind='Y' then T1.Country Else '' End,''))
, ... etc.
,max(coalesce(CASE T3.Legal_Ind='Y' then T1.Country Else '' End,''))
, ... etc.
from Table2 T2
left join Table3 T3 on T2.Member_Key=T3.Member_Key
left join Table1 T1A on T3.Address_key = T1.Address_Key
group by Member_Key, Member_id