sequential joins - Mailing list pgsql-sql

From Oleg Lebedev
Subject sequential joins
Date
Msg-id 3C7FE2DF.2AF0A2BC@waterford.org
Whole thread Raw
Responses Re: sequential joins  ("Josh Berkus" <josh@agliodbs.com>)
List pgsql-sql
You guys and gals were really helpful!
I hope you can help me with this problem too.
I have an Activity record, that has fields like artist, designer,
programmer containing ids of users from User table, which are assigned
to the current activity.
What I need to do is create a view, that would contain all the Activity
information, except artist, designer, and programmer should be filled
out with corresponding usernames from the User table.
Here is one way to do this:
Schemas:
Activity: name, artist, designer, programmer
User: username, objectid

SELECT name, artistname, designername, programmername
FROM
(SELECT *
FROM activity a
LEFT OUTER JOIN (SELECT username AS artistname, objectid AS userid FROM
User) n1
ON a.artist = n1.userid
LEFT OUTER JOIN (SELECT username AS designername, objectid AS userid
FROM User) n2
ON a.designer = n2.userid
LEFT OUTER JOIN (SELECT username AS programmername, objectid AS userid
FROM User) n3
ON a.programmer = n3.userid) names;

I wonder if there is a better way to do this. Maybe using CASE WHEN THEN
ELSE END clause to avoid multiple scans?
thanks,

Oleg



pgsql-sql by date:

Previous
From: "Josh Berkus"
Date:
Subject: Re: sub SELECT
Next
From: "Josh Berkus"
Date:
Subject: Re: sequential joins