JOIN TO GET DISTINCT ROWS

Hi,
i have two tables table_a
row_id country  par_row_id
1       NZ        1
2       NZ        1
3       AU        2
4       AU        2
5       US        3
6       NZ        1
7       NZ        1


table_b
row_id   name
1         abc    
2         ssss
3          kkk
4         lll
5        ffff

want to join table_a with b based on b.row_id=a.par_row_id and want to get only distinct rows from table_a

count should be 5


row_id   name  country
1         abc    NZ
2         ssss   AU
3          kkk   US
4         lll
5        ffff

Thanks
sam2929Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Use this query...

select tb1.row_id,tb1.name,tb2.country
from table_b tb1
left join (select distinct country ,par_row_id from table_b) tb2 on tb1.row_id=tb2.par_row_id

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperCommented:
try (using cross apply):
SELECT distinct a.row_id , a.country, a.par_row_id, b.name
FROM table_A as a
CROSS APPLY (SELECT row_id , name FROM table_b as b
WHERE a.row_id = b.row_id) AS b;

Open in new window


returns:
1      NZ      1      AB
2      NZ      1      SS
3      AU      2      KK
4      AU      2      LL
5      US      3      FF

Tested using:
create table #table_a (row_id int, country char(2),  par_row_id int);
create table #table_b (row_id int, name varchar(2));
select * from #table_a;
select * from #table_b;


insert into #table_a(row_id , country ,  par_row_id ) values 
(1, 'NZ', 1),
(2, 'NZ', 1),
(3, 'AU', 2),
(4, 'AU', 2),
(5, 'US', 3),
(6, 'NZ', 1),
(7, 'NZ', 1);

insert into #table_b(row_id , name ) values  
(1, 'AB'),
(2, 'SS'),
(3, 'KK'),
(4, 'LL'),
(5, 'FF');

Open in new window


try (using outer apply):
SELECT distinct a.row_id , a.country, a.par_row_id, b.name
FROM #table_A as a
outer APPLY (SELECT row_id , name FROM #table_b as b
WHERE a.row_id = b.row_id) AS b;

Open in new window


returns:
1      NZ      1      AB
2      NZ      1      SS
3      AU      2      KK
4      AU      2      LL
5      US      3      FF
6      NZ      1      NULL
7      NZ      1      NULL
0
Scott PletcherSenior DBACommented:
Very minor correction(s)/clarifications to Saurabh's code, please NO points for me:

select b.row_id,b.name,a.country
from table_b b
left join (select distinct country ,par_row_id from table_a) a on b.row_id=a.par_row_id
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mike EghtebasDatabase and Application DeveloperCommented:
I think the following may work for you (but returns 6 rows):
SELECT distinct  a.country, b.name
FROM #table_A as a
outer APPLY (SELECT row_id , name FROM #table_b as b
WHERE a.row_id = b.row_id) AS b;

Open in new window

0
Saurabh Singh TeotiaCommented:
Thanks Scott..My bad i missed that.. :-)

Saurabh...
0
Mike EghtebasDatabase and Application DeveloperCommented:
I had some errors but this one works ok:
Select distinct b.row_id, b.name, a.country 
FROM #table_b as b
outer APPLY (SELECT  country FROM #table_a
WHERE par_row_id = b.row_id) AS a;

Open in new window


1      AB      NZ
2      SS      AU
3      KK      US
4      LL      NULL
5      FF      NULL

But have in mind the solution from Saurabh was the first and gets the points.

Thanks,

Mike
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
sam2929, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.