I am having a hard time understanding the concepts of join when there are extra level of details on second table. I have two tables as shown here, table 1 and table 2.
Let's say I want to return the DOB
from table 2 alongside data from table 1:
select
t1.PERSON ID,
t1.Celeb First Name,
t1.Celeb Last Name,
t2.DOB
from
table 1 as t1
left join
table 2 t2 on t1.personid = t2.personid
I get duplicate rows even when I do a group by. What is the solution to only return one row? I don't want to use distinct as that slows my query a lot.
select
t1.PERSON ID,
t1.Celeb First Name,
t1.Celeb Last Name,
t2.DOB
from
table 1 as t1
left join
table 2 t2 on t1.personid = t2.personid
group by
t1.PERSON ID,
t1.Celeb First Name,
t1.Celeb Last Name,
t2.DOB
2条答案
按热度按时间0pizxfdo1#
You're getting more than one row per person because there is more than one value for DOB according to your data; Person 1 having a DOB of '1/1/2020' and NULL. You could filter out these rows with the following WHERE clause:
This will cause problems if you wanted to use the [MOVIE ACCESS] later though.
w8ntj3qf2#
Use one of the aggregation functions like
max()
ormin()
to retrieve just one row, in your case you will need to usemax()
: