From: | "Josh Berkus" <josh(at)agliodbs(dot)com> |
---|---|
To: | "Glenn MacGregor" <gtm(at)oracom(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Complex query help please |
Date: | 2001-11-28 00:24:38 |
Message-ID: | web-514821@davinci.ethosmedia.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Glenn,
> I am new to sql and have the need for a complex query. I have
> figured out how to do it in two queries but I would like to get it
> down to one. I will give you my queries and hopefully some can give
> me some assistance.
> Thanks
Easy. Take your second query, and everywhere you have "ttable1" replace
it with a sub-select which is the first query. Thus:
Query 1:
create temp table ttable1 as select xpressuser.username,
xpressvimpermission.commandaccess, xpressvimpermission.eventaccess from
xpressvim left join xpressvimpermission using (vimid) left join
xpressgroup using (groupid) left join xpressuser using (groupid) where
xpressvim.vimname = 'testvim2';
creates a temp table.
select xpressuser.username, xpressvimpermission.commandaccess,
xpressvimpermission.eventaccess from xpressvim left join
xpressvimpermission using (vimid) left join xpressgroup using (groupid)
left join xpressuser using (groupid) where xpressvim.vimname =
'testvim2'
union select xpressuser.username, xpressgroup.commandaccess,
xpressgroup.eventaccess from xpressgroup left join xpressuser using
(groupid) where xpressuser.username not in (select xpressuser.username
from xpressvim left join xpressvimpermission using (vimid) left join
xpressgroup using (groupid) left join xpressuser using (groupid) where
xpressvim.vimname = 'testvim2') and (xpressgroup.commandaccess != 'n' or
xpressgroup.eventaccess != 'f');
You can play with different query structures (WHERE NOT EXISTS, etc) for
performance reasons. Certainly you should save the above as a view to
get optimization.
ALso, make sure that you're using Postgres 7.1.0 or above.
-Josh
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
From | Date | Subject | |
---|---|---|---|
Next Message | Stephan Szabo | 2001-11-28 00:32:03 | Re: PL/pgSQL examples NOT involving functions |
Previous Message | Josh Berkus | 2001-11-28 00:01:36 | Re: PL/pgSQL examples NOT involving functions |