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
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;
I Have 3 tables as below. In table1, if value in column id is present then join that column with other tables, if null I want to join with ref column from table 1 with other tables. What is the best way to do this
table1
Name id ref
abc 123
abc 456
edc 345 432
asd 678
table 2
city id ref
NY 123
WA 875
CA 345 432
SA 678
table 3
city orders
NY 78954
WA 123546
CA 789
SA 1
I want below result: ABC has 123 in id hence NY. ASD has 678, so considered ref column in table1 to join with other 2 tables
Name city order
ABC NY 78954
EDC CA 789
ASD SA 1
You can use left joins for this purpose:
select t1.*, t3.city, t3.count
from table1 t1 left join
table2 t2
on t1.id = t2.id left join
table2 t2r
on t1.ref = t2.ref and t1.id is null left join
table3 t3
on t3.city = coalesce(t1.city, t2.city);
Note: This returns all rows from table1, even those with no match in either column. If this is an issue, add:
where t2.id is not null or t2.ref is not null
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 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.
I have following tables:
Table2
StudentNumbers ExamType
------------------------
1234 1
2343 2
3345 5
3454 1
5465 2
...
Table1
StudentNumbers ExamType ExamDate School Area Info
------------------------------------------------------------------
1234 1 0825 warren ny 0x504B03042D0
1234 1 0829 north nj 0x63D86E1FFFF
1233 2 0921 north nj 0xA001400646F
2343 1 0922 warren ny 0x01400646174
2343 1 0925 north ny 0x100100070se
...
I am trying to write a query to get following results:
StudentNumbers ExamType ExamDate School Area Info
-----------------------------------------------------------------
1234 1 0829 north nj 0x63D86E1FFFF
2343 1 0925 north ny 0x100100070se
I wrote following query:
Select t1.StudentNumbers, t1.ExamDate, t1.School, t1.Info, t1.ExamType
from Table1 as t1
Join(
Select ts.StudentNumbers, max(ts.ExamDate) as ExamDate
from Table2 as ts
Join Table1 as pl on
ts.StudentNumbers = pl.StudentNumbers where ts.ExamType = pl.ExamType
group by ts.StudentNumbers
) as t2 on t1.StudentNumbers = t2.StudentNumbers
and t1.ExamDate = t2.ExamDate
Above query works and gives me the result i want that is max ExamDate for particular ExamType based on Table2. But is using multiple joins best way in this aggregate method? Or is there a cleaner option.
Your query is good, but I think to use Table2 table outside subquery would be better option.
SELECT *
FROM (
SELECT StudentNumbers, ExamType, MAX(ExamDate) AS ExamDate
FROM Table1
GROUP BY StudentNumbers, ExamType
) t1 JOIN Table1 t2 ON t1.StudentNumbers = t2.StudentNumbers
AND t1.ExamType = t2.ExamType AND t1.ExamDate = t2.ExamDate
WHERE EXISTS(
SELECT 1
FROM Table2 t3
WHERE t1.StudentNumbers = t3.StudentNumbers
AND t1.ExamType = t3.ExamType
)
SELECT
t1.*
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.StudentNumbers = t2.StudentNumbers
WHERE t1.ExamDate = (SELECT MAX(ExamDate) FROM Table1 WHERE StudentNumbers = t1.StudentNumbers)
ORDER BY t1.StudentNumbers ASC
Might not be the most efficient solution though.