pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
OT: seeking query help, where?
13+ messages / 11 participants
[nested] [flat]

* OT: seeking query help, where?
@ 2003-01-16 22:15 Tim Lynch <admin+pgsqladmin@thirdage.com>
  2003-01-16 22:47 ` Re: OT: seeking query help, where? Steve Crawford <scrawford@pinpointresearch.com>
  2003-01-16 23:12 ` Re: OT: seeking query help, where? Robert Treat <xzilla@users.sourceforge.net>
  2003-01-16 23:29 ` Re: OT: seeking query help, where? Bruno Wolff III <bruno@wolff.to>
  2003-01-17 00:24 ` Re: OT: seeking query help, where? Nigel J. Andrews <nandrews@investsystems.co.uk>
  2003-01-17 07:37 ` Re: OT: seeking query help, where? Tim Ellis <pvspam-postgres@hacklab.net>
  2003-01-17 13:57 ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
  0 siblings, 6 replies; 13+ messages in thread

From: Tim Lynch @ 2003-01-16 22:15 UTC (permalink / raw)
  To: pgsql-admin

First, sorry for the OT, flame me off-list!

I'm a sysadmin being impressed into dba service. i've been getting along
pretty well writing queries and making reports, but i've got some questions.
suggestions for a good list/forum for help?

from two tables both with email_addr columns, i want a distinct list of all
email_addrs in one column. what i do now is select distinct on each and then
sort -u the results.

thanks in advance
-t




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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
@ 2003-01-16 22:47 ` Steve Crawford <scrawford@pinpointresearch.com>
  2003-01-17 01:06   ` Re: OT: seeking query help, where? Tim Lynch <admin@thirdage.com>
  5 siblings, 1 reply; 13+ messages in thread

From: Steve Crawford @ 2003-01-16 22:47 UTC (permalink / raw)
  To: Tim Lynch <admin+pgsqladmin@thirdage.com>; pgsql-admin

I've never really used this site but it might be of interest: 
http://www.dbforums.com/

You may also want to check out "SQL Queries for Mere Mortals" 
(Hernandez/Viescas - Addison Wesley)

-Steve


On Thursday 16 January 2003 2:15 pm, Tim Lynch wrote:
> First, sorry for the OT, flame me off-list!
>
> I'm a sysadmin being impressed into dba service. i've been getting along
> pretty well writing queries and making reports, but i've got some
> questions. suggestions for a good list/forum for help?
>
> from two tables both with email_addr columns, i want a distinct list of all
> email_addrs in one column. what i do now is select distinct on each and
> then sort -u the results.
>
> thanks in advance
> -t
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)



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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
  2003-01-16 22:47 ` Re: OT: seeking query help, where? Steve Crawford <scrawford@pinpointresearch.com>
@ 2003-01-17 01:06   ` Tim Lynch <admin@thirdage.com>
  0 siblings, 0 replies; 13+ messages in thread

From: Tim Lynch @ 2003-01-17 01:06 UTC (permalink / raw)
  To: pgsql-admin; +Cc: Steve Crawford <scrawford@pinpointresearch.com>; Robert Treat <xzilla@users.sourceforge.net>; Devinder K Rajput <Devinder.Rajput@ipaper.com>; Bruno Wolff III <bruno@wolff.to>; Nigel J. Andrews <nandrews@investsystems.co.uk>

UNION! thanks everyone!

i'll take this stuff up on the pgsql-sql list in the future.

union does intra- and inter- table distinct, notice `red' and `two':

test=> select str from foo ;
 str
-----
 one
 two
 two
 red
(4 rows)

test=> select str from bar ;
  str
--------
 red
 orange
 yellow
 green
 blue
 indigo
 violet
 red
(8 rows)

test=> select str from foo union ( select str from bar ) ;
  str
--------
 blue
 green
 indigo
 one
 orange
 red
 two
 violet
 yellow
(9 rows)




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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
@ 2003-01-16 23:12 ` Robert Treat <xzilla@users.sourceforge.net>
  5 siblings, 0 replies; 13+ messages in thread

From: Robert Treat @ 2003-01-16 23:12 UTC (permalink / raw)
  To: Tim Lynch <admin+pgsqladmin@thirdage.com>; +Cc: pgsql-admin

there are several ways to do this, one example would be:

select distinct(email_addr) from table1
union
select distinct(email_addr) from table2

You can ask questions like these on the pgsql-newbies or pgsql-sql lists
if you'd like (though generally you shouldn't get flamed no matter which
group you post to)

Robert Treat

On Thu, 2003-01-16 at 17:15, Tim Lynch wrote:
> First, sorry for the OT, flame me off-list!
> 
> I'm a sysadmin being impressed into dba service. i've been getting along
> pretty well writing queries and making reports, but i've got some questions.
> suggestions for a good list/forum for help?
> 
> from two tables both with email_addr columns, i want a distinct list of all
> email_addrs in one column. what i do now is select distinct on each and then
> sort -u the results.
> 
> thanks in advance
> -t
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)






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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
@ 2003-01-16 23:29 ` Bruno Wolff III <bruno@wolff.to>
  5 siblings, 0 replies; 13+ messages in thread

From: Bruno Wolff III @ 2003-01-16 23:29 UTC (permalink / raw)
  To: Tim Lynch <admin+pgsqladmin@thirdage.com>; +Cc: pgsql-admin

On Thu, Jan 16, 2003 at 14:15:22 -0800,
  Tim Lynch <admin+pgsqladmin@thirdage.com> wrote:
> First, sorry for the OT, flame me off-list!
> 
> I'm a sysadmin being impressed into dba service. i've been getting along
> pretty well writing queries and making reports, but i've got some questions.
> suggestions for a good list/forum for help?
> 
> from two tables both with email_addr columns, i want a distinct list of all
> email_addrs in one column. what i do now is select distinct on each and then
> sort -u the results.

Use the union operator.



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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
@ 2003-01-17 00:24 ` Nigel J. Andrews <nandrews@investsystems.co.uk>
  5 siblings, 0 replies; 13+ messages in thread

From: Nigel J. Andrews @ 2003-01-17 00:24 UTC (permalink / raw)
  To: Tim Lynch <admin+pgsqladmin@thirdage.com>; +Cc: pgsql-admin

On Thu, 16 Jan 2003, Tim Lynch wrote:

> First, sorry for the OT, flame me off-list!
> 
> I'm a sysadmin being impressed into dba service. i've been getting along
> pretty well writing queries and making reports, but i've got some questions.
> suggestions for a good list/forum for help?
> 
> from two tables both with email_addr columns, i want a distinct list of all
> email_addrs in one column. what i do now is select distinct on each and then
> sort -u the results.
> 

I like it! My sort of solution. Only I don't know the -u switch, I'd have done
`cat blah* | sort | uniq` but it's obviously the same idea.

As for the query that would do it, I believe that's already been answered I
just thought I'd stand up for the good old fashioned unix ways. :)


-- 
Nigel J. Andrews




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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
@ 2003-01-17 07:37 ` Tim Ellis <pvspam-postgres@hacklab.net>
  5 siblings, 0 replies; 13+ messages in thread

From: Tim Ellis @ 2003-01-17 07:37 UTC (permalink / raw)
  To: pgsql-admin

On Thu, 2003-01-16 at 17:15, Tim Lynch wrote:
> from two tables both with email_addr columns, i want a distinct list of all
> email_addrs in one column. what i do now is select distinct on each and then
> sort -u the results.

You want either pgsql-sql or pgsql-novice, both of which are mailing
lists for PostgreSQL. The former would be exactly the correct forum.
Odds are someone in this list will answer you anyway with something
mostly-useful.

I think the UNION clause will help you to your eventual goal.

--
Tim Ellis
Senior Database Architect





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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
@ 2003-01-17 13:57 ` Andrew Perrin <clists@perrin.socsci.unc.edu>
  2003-01-17 15:12   ` Re: OT: seeking query help, where? Bruno Wolff III <bruno@wolff.to>
  5 siblings, 1 reply; 13+ messages in thread

From: Andrew Perrin @ 2003-01-17 13:57 UTC (permalink / raw)
  To: Tim Lynch <admin+pgsqladmin@thirdage.com>; +Cc: pgsql-admin

I'd suggest something similar to, but slightly different from, what others
have:

SELECT DISTINCT email FROM (SELECT email FROM table_1 UNION SELECT
	email FROM table_2) AS combined;

that way you avoid duplicates across tables.

----------------------------------------------------------------------
Andrew J Perrin - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
clists@perrin.socsci.unc.edu * andrew_perrin (at) unc.edu


On Thu, 16 Jan 2003, Tim Lynch wrote:

> First, sorry for the OT, flame me off-list!
> 
> I'm a sysadmin being impressed into dba service. i've been getting along
> pretty well writing queries and making reports, but i've got some questions.
> suggestions for a good list/forum for help?
> 
> from two tables both with email_addr columns, i want a distinct list of all
> email_addrs in one column. what i do now is select distinct on each and then
> sort -u the results.
> 
> thanks in advance
> -t
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
> 




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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
  2003-01-17 13:57 ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
@ 2003-01-17 15:12   ` Bruno Wolff III <bruno@wolff.to>
  2003-01-17 15:05     ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
  0 siblings, 1 reply; 13+ messages in thread

From: Bruno Wolff III @ 2003-01-17 15:12 UTC (permalink / raw)
  To: Andrew Perrin <clists@perrin.socsci.unc.edu>; +Cc: Tim Lynch <admin+pgsqladmin@thirdage.com>; pgsql-admin

On Fri, Jan 17, 2003 at 08:57:14 -0500,
  Andrew Perrin <clists@perrin.socsci.unc.edu> wrote:
> I'd suggest something similar to, but slightly different from, what others
> have:
> 
> SELECT DISTINCT email FROM (SELECT email FROM table_1 UNION SELECT
> 	email FROM table_2) AS combined;
> 
> that way you avoid duplicates across tables.

The union operator already removes duplicates.



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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
  2003-01-17 13:57 ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
  2003-01-17 15:12   ` Re: OT: seeking query help, where? Bruno Wolff III <bruno@wolff.to>
@ 2003-01-17 15:05     ` Andrew Perrin <clists@perrin.socsci.unc.edu>
  2003-01-17 15:54       ` Re: OT: seeking query help, where? Ross J. Reedstrom <reedstrm@rice.edu>
  0 siblings, 1 reply; 13+ messages in thread

From: Andrew Perrin @ 2003-01-17 15:05 UTC (permalink / raw)
  To: Bruno Wolff III <bruno@wolff.to>; +Cc: Tim Lynch <admin+pgsqladmin@thirdage.com>; pgsql-admin

Hey, thanks - nice to learn something.

ap

----------------------------------------------------------------------
Andrew J Perrin - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
clists@perrin.socsci.unc.edu * andrew_perrin (at) unc.edu


On Fri, 17 Jan 2003, Bruno Wolff III wrote:

> On Fri, Jan 17, 2003 at 08:57:14 -0500,
>   Andrew Perrin <clists@perrin.socsci.unc.edu> wrote:
> > I'd suggest something similar to, but slightly different from, what others
> > have:
> > 
> > SELECT DISTINCT email FROM (SELECT email FROM table_1 UNION SELECT
> > 	email FROM table_2) AS combined;
> > 
> > that way you avoid duplicates across tables.
> 
> The union operator already removes duplicates.
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
> 




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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
  2003-01-17 13:57 ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
  2003-01-17 15:12   ` Re: OT: seeking query help, where? Bruno Wolff III <bruno@wolff.to>
  2003-01-17 15:05     ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
@ 2003-01-17 15:54       ` Ross J. Reedstrom <reedstrm@rice.edu>
  2003-01-17 16:24         ` Re: OT: seeking query help, where? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 13+ messages in thread

From: Ross J. Reedstrom @ 2003-01-17 15:54 UTC (permalink / raw)
  To: Andrew Perrin <clists@perrin.socsci.unc.edu>; +Cc: Bruno Wolff III <bruno@wolff.to>; Tim Lynch <admin+pgsqladmin@thirdage.com>; pgsql-admin

On Fri, 17 Jan 2003, Bruno Wolff III wrote:
> On Fri, Jan 17, 2003 at 08:57:14 -0500,
>   Andrew Perrin <clists@perrin.socsci.unc.edu> wrote:
> > 
> > that way you avoid duplicates across tables.
> 
> The union operator already removes duplicates.

Right, which means everyone's UNION queries have too many DISTINCTs
in them:

SELECT email from table1
UNION 
SELECT email from table2 ; 

should do it.

Here's proof: note the duplicate 'foo' and 'bar':

test=# select * from table1;
      email      
-----------------
 foo@example.com
 bar@example.com
 bar@example.com
(3 rows)

test=# select * from table2;
      email       
------------------
 quux@example.com
 foo@example.com
(2 rows)

test=# select email 

test=# select email from table1 union select email from table2; 
      email       
------------------
 bar@example.com
 foo@example.com
 quux@example.com
(3 rows)

Ross
-- 
Ross Reedstrom, Ph.D.                                 reedstrm@rice.edu
Research Scientist                                  phone: 713-348-6166
The Connexions Project      http://cnx./rice.edu      fax: 713-348-6182
Rice University MS-39
Houston, TX 77005



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

* Re: OT: seeking query help, where?
  2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
  2003-01-17 13:57 ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
  2003-01-17 15:12   ` Re: OT: seeking query help, where? Bruno Wolff III <bruno@wolff.to>
  2003-01-17 15:05     ` Re: OT: seeking query help, where? Andrew Perrin <clists@perrin.socsci.unc.edu>
  2003-01-17 15:54       ` Re: OT: seeking query help, where? Ross J. Reedstrom <reedstrm@rice.edu>
@ 2003-01-17 16:24         ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 13+ messages in thread

From: Tom Lane @ 2003-01-17 16:24 UTC (permalink / raw)
  To: Ross J. Reedstrom <reedstrm@rice.edu>; +Cc: Andrew Perrin <clists@perrin.socsci.unc.edu>; Bruno Wolff III <bruno@wolff.to>; Tim Lynch <admin+pgsqladmin@thirdage.com>; pgsql-admin

"Ross J. Reedstrom" <reedstrm@rice.edu> writes:
> On Fri, 17 Jan 2003, Bruno Wolff III wrote:
>> The union operator already removes duplicates.

> Right, which means everyone's UNION queries have too many DISTINCTs
> in them:

Relevant to this thread: if you don't want duplicate removal, write
UNION ALL.  This is considerably cheaper than UNION, so it's a good
thing to keep in mind.

Examples:

regression=# explain analyze select unique1 from tenk1 union all select unique2 from tenk1;
                                                       QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
 Append  (cost=0.00..916.00 rows=20000 width=4) (actual time=0.20..1416.60 rows=20000 loops=1)
   ->  Subquery Scan "*SELECT* 1"  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.18..549.34 rows=10000 loops=1)
         ->  Seq Scan on tenk1  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.15..279.58 rows=10000 loops=1)
   ->  Subquery Scan "*SELECT* 2"  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.14..548.90 rows=10000 loops=1)
         ->  Seq Scan on tenk1  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.11..278.33 rows=10000 loops=1)
 Total runtime: 1570.02 msec
(6 rows)

regression=# explain analyze select unique1 from tenk1 union select unique2 from tenk1;
                                                             QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=2344.77..2444.77 rows=20000 width=4) (actual time=2881.85..3477.51 rows=10000 loops=1)
   ->  Sort  (cost=2344.77..2394.77 rows=20000 width=4) (actual time=2881.82..3169.29 rows=20000 loops=1)
         Sort Key: unique1
         ->  Append  (cost=0.00..916.00 rows=20000 width=4) (actual time=0.21..1590.55 rows=20000 loops=1)
               ->  Subquery Scan "*SELECT* 1"  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.19..570.31 rows=10000 loops=1)
                     ->  Seq Scan on tenk1  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.15..293.18 rows=10000 loops=1)
               ->  Subquery Scan "*SELECT* 2"  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.15..698.43 rows=10000 loops=1)
                     ->  Seq Scan on tenk1  (cost=0.00..458.00 rows=10000 width=4) (actual time=0.12..419.75 rows=10000 loops=1)
 Total runtime: 3574.98 msec
(9 rows)

The sort-and-unique phases are what implement duplicate removal, and as
you can see they add a good deal to the cost of the query.

			regards, tom lane



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

* Re: OT: seeking query help, where?
@ 2003-01-16 23:17 Devinder K Rajput <Devinder.Rajput@ipaper.com>
  0 siblings, 0 replies; 13+ messages in thread

From: Devinder K Rajput @ 2003-01-16 23:17 UTC (permalink / raw)
  To: Tim Lynch <admin+pgsqladmin@thirdage.com>; +Cc: pgsql-admin; pgsql-admin-owner@postgresql.org

you should be able to do something like:

select distinct email from test1
union (select distinct email from test2);

regards,

Devinder Rajput
Stores Division Corporate Offices
Chicago, IL
(773) 442-6474




"Tim Lynch" <admin+pgsqladmin@thirdage.com>
Sent by: pgsql-admin-owner@postgresql.org
01/16/2003 04:15 PM

 
        To:     pgsql-admin@postgresql.org
        cc: 
        Subject:        [ADMIN] OT: seeking query help, where?


First, sorry for the OT, flame me off-list!

I'm a sysadmin being impressed into dba service. i've been getting along
pretty well writing queries and making reports, but i've got some 
questions.
suggestions for a good list/forum for help?

from two tables both with email_addr columns, i want a distinct list of 
all
email_addrs in one column. what i do now is select distinct on each and 
then
sort -u the results.

thanks in advance
-t


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

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


end of thread, other threads:[~2003-01-17 16:24 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-01-16 22:15 OT: seeking query help, where? Tim Lynch <admin+pgsqladmin@thirdage.com>
2003-01-16 22:47 ` Steve Crawford <scrawford@pinpointresearch.com>
2003-01-17 01:06   ` Tim Lynch <admin@thirdage.com>
2003-01-16 23:12 ` Robert Treat <xzilla@users.sourceforge.net>
2003-01-16 23:29 ` Bruno Wolff III <bruno@wolff.to>
2003-01-17 00:24 ` Nigel J. Andrews <nandrews@investsystems.co.uk>
2003-01-17 07:37 ` Tim Ellis <pvspam-postgres@hacklab.net>
2003-01-17 13:57 ` Andrew Perrin <clists@perrin.socsci.unc.edu>
2003-01-17 15:12   ` Bruno Wolff III <bruno@wolff.to>
2003-01-17 15:05     ` Andrew Perrin <clists@perrin.socsci.unc.edu>
2003-01-17 15:54       ` Ross J. Reedstrom <reedstrm@rice.edu>
2003-01-17 16:24         ` Tom Lane <tgl@sss.pgh.pa.us>
2003-01-16 23:17 Re: OT: seeking query help, where? Devinder K Rajput <Devinder.Rajput@ipaper.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