pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Complex query help please
2+ messages / 2 participants
[nested] [flat]

* Complex query help please
@ 2001-11-27 22:23  Glenn MacGregor <gtm@oracom.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Glenn MacGregor @ 2001-11-27 22:23 UTC (permalink / raw)
  To: pgsql-sql

Hi All,

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

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.

Query 2:
select * from ttable1 union select xpressuser.username, xpressgroup.commandaccess,  xpressgroup.eventaccess from xpressgroup left join xpressuser using (groupid) where xpressuser.username not in (select username from ttable1) and (xpressgroup.commandaccess != 'n' or  xpressgroup.eventaccess != 'f');

So the idea is I have a default set of permissions that users get, these permissions can be overridden on each vim for each group(user).  The first query results in a table which has all the overridden permissions.
The second query results in a table which has all the default permissions unioned with the previous table and not in the previous table.

Is there any way to do this one query or a function?

    Thanks

            Glenn

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Complex query help please
@ 2001-11-28 00:24  Josh Berkus <josh@agliodbs.com>
  parent: Glenn MacGregor <gtm@oracom.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Josh Berkus @ 2001-11-28 00:24 UTC (permalink / raw)
  To: Glenn MacGregor <gtm@oracom.com>; +Cc: 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@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-11-28 00:24 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-11-27 22:23 Complex query help please Glenn MacGregor <gtm@oracom.com>
2001-11-28 00:24 ` Josh Berkus <josh@agliodbs.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox