| From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
|---|---|
| To: | Stuart Rison <stuart(at)ludwig(dot)ucl(dot)ac(dot)uk> |
| Cc: | pgsql-sql(at)postgreSQL(dot)org, matthew(at)venux(dot)net |
| Subject: | Re: [SQL] DISTINCT count(*), possible? |
| Date: | 1999-06-16 14:24:39 |
| Message-ID: | 24839.929543079@sss.pgh.pa.us |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-sql |
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
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Roland_DUBOULOZ | 1999-06-16 14:35:42 | date |
| Previous Message | The Hermit Hacker | 1999-06-16 13:19:01 | Re: [HACKERS] Postgres mailing lists |