Re: [SQL] Counting Distinct Records

2004-11-17 Thread Thomas F . O'Connell
The specific problem I'm trying to solve involves a user table with some history. Something like this: create table user_history ( user_id int event_time_stamp timestamp ); I'd like to be able to count the distinct user_ids in this table, even if it were joined to other tables.

Re: [SQL] Counting Distinct Records

2004-11-17 Thread Stephan Szabo
On Tue, 16 Nov 2004, Thomas F.O'Connell wrote: > Hmm. I was more interested in using COUNT( * ) than DISTINCT *. > > I want a count of all rows, but I want to be able to specify which > columns are distinct. I'm now a bit confused about exactly what you're looking for in the end. Can you give a s

Re: [SQL] Counting Distinct Records

2004-11-16 Thread Thomas F.O'Connell
Hmm. I was more interested in using COUNT( * ) than DISTINCT *. I want a count of all rows, but I want to be able to specify which columns are distinct. That's definitely an interesting approach, but testing doesn't show it to be appreciably faster. If I do a DISTINCT *, postgres will attempt t

Re: [SQL] Counting Distinct Records

2004-11-16 Thread Stephan Szabo
On Tue, 16 Nov 2004, Thomas F.O'Connell wrote: > Is there another way to accomplish what the former is doing, then? The only thing I can think of is a subselect in from that uses distinct. select count(*) from (select distinct ...) foo That also theoretically allows you to use select distinct *

Re: [SQL] Counting Distinct Records

2004-11-16 Thread Thomas F.O'Connell
Is there another way to accomplish what the former is doing, then? For practical reasons, I'd like to come up with something better. For theoretical curiosity, I'd like to know whether there's a way to combine COUNT and DISTINCT that still allows one to reference * rather than naming specific col

Re: [SQL] Counting Distinct Records

2004-11-16 Thread Stephan Szabo
On Tue, 16 Nov 2004, Thomas F.O'Connell wrote: > I am wondering whether the following two forms of SELECT statements are > logically equivalent: > > SELECT COUNT( DISTINCT table.column ) ... > > and > > SELECT DISTINCT COUNT( * ) ... Not in general. The former counts how many distinct table.colu

[SQL] Counting Distinct Records

2004-11-16 Thread Thomas F . O'Connell
I am wondering whether the following two forms of SELECT statements are logically equivalent: SELECT COUNT( DISTINCT table.column ) ... and SELECT DISTINCT COUNT( * ) ... If they are the same, then why is the latter query much slower in postgres when applied to the same FROM and WHERE clauses?