Join Table Twice Each With Two Different Values for Same Field

Connie

I have a record that has two people records associated with it. The people records are sellers (peoplecode = 20)and clients (peoplecode = 1). I want the city of both the seller and the client. I thought it would be straight forward, but cannot seem to get it to work - I get no results. The Folderpeople table just relates my folder to my people.

select f.folderid, f.foldername, fp.peoplecode, 
p.addrcity as clientcity, p2.addrcity as sellercity 
from folder  f
join folderpeople fp on fp.folderid = f.folderid 
join people p on fp.peopleid = p.peopleid and fp.peoplecode = 1 
join people p2 on fp.peopleid = p2.peopleid and fp.peoplecode = 20 ; 
Connie

I jumped the gun - Randy's answer gave results - but gave me two rows for everyone. But it pointed me in the right direction. When I combined Randy's solution with mdem7's solution, I found a solution that worked.

with sellers as (select fp.folderid, p.peopleid, fp.peoplecode, p.addrcity, 
     p.addrpostal from folderpeople fp , people p where fp.peopleid = p.peopleid and 
     fp.peoplecode = 20), 
clients as 
     (select fp2.folderid, p2.peopleid, fp2.peoplecode, p2.addrcity, p2.addrprovince, 
     p2.addrpostal, p2.namelast, p2.namefirst from folderpeople fp2, people p2  where 
     fp2.peopleid = p2.peopleid and fp2.peoplecode = 1)
select c.namelast as ClientLast, c.namefirst as ClientFirst, f.issuedate, 
     c.addrcity as "Client City", s.addrcity as "Seller City"
     from folder  f
        left join sellers s on f.folderid = s.folderid  
        left join clients c on f.folderid = c.folderid; 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JOIN the Same Table.Field Twice but for Different Records

Join two table with two different Foreign Key for get same field

Join same table twice for count in different columns

Joining the same table twice with different values

join two columns with same title but in different table

MySQL join same table twice to fetch value based on different IDs

Unable to JOIN the same table twice based on two conditions

How to join two tables without repeating the same rows for different values in table

How to join two values from different columns of the same table in Sqlite3?

Join the same table twice with conditions

How to join on same table twice?

How to join the same table twice

SQL Two JOINS same table different values

SQL - Join two fields in the same table to a single field in another table

Inner Join to Same Table Twice on same column

Join Same Column from Same Table Twice

How to join two tables with same primary key name but different values

Left Join the same table based on two different fields?

Join on two table with same primary key but different data

Join two rows by different ids from a same table

Shortening a LEFT JOIN with two different conditions on same Table

Join two query in the same table on different month and sum value

Unable to do a JOIN when having the same table with two different names

Compare values from the same field in two different columns

Get two values from same table using SELECT - JOIN

SQL query to join to the same parent table twice

JOIN same table twice with aliases on SQLAlchemy

How to join same table twice using PXSelectJoinOrderBy

Join same table twice to find missing data