pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Stuart Rison <stuart@ludwig.ucl.ac.uk>
Cc: pgsql-sql@postgreSQL.org
Cc: matthew@venux.net
Subject: Re: [SQL] DISTINCT count(*), possible?
Date: Wed, 16 Jun 1999 10:24:39 -0400
Message-ID: <24839.929543079@sss.pgh.pa.us> (raw)
In-Reply-To: <v04020a00b38d1337fb45@[128.40.242.190]>

Another way is

	SELECT DISTINCT field INTO temp_table FROM mytable;
	SELECT count(*) FROM temp_table;
	DROP TABLE temp_table;

which is arguably more efficient than the previous solution
for large tables --- it should involve O(n log n) work rather
than O(n^2).  For a small table, the overhead of creating and
dropping a table might overshadow the actual work, though.

In 6.5 you can use "INTO TEMP temp_table" and avoid worrying
about having to invent distinct temp table names for concurrent
users of the database.

The SQL-standard "SELECT count(DISTINCT field)" would be even nicer,
of course, but I dunno when someone will get around to it...

			regards, tom lane



view thread (8+ messages)  latest in thread

Message-ID: <24839.929543079@sss.pgh.pa.us>
Permalink:  ../24839.929543079@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/24839.929543079@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, stuart@ludwig.ucl.ac.uk, matthew@venux.net
  Subject: Re: [SQL] DISTINCT count(*), possible?
  In-Reply-To: <24839.929543079@sss.pgh.pa.us>
  References: <v04020a00b38d1337fb45@[128.40.242.190]> <24839.929543079@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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