Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 92BCE1337B47 for ; Fri, 26 Nov 2010 07:49:31 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 49098-01 for ; Fri, 26 Nov 2010 11:49:24 +0000 (UTC) X-Greylist: delayed 00:09:55.099159 by SQLgrey-1.7.6 Received: from smtp.go-wipex.it (mx1.go-wipex.it [89.202.235.7]) by mail.postgresql.org (Postfix) with ESMTP id 44A591337999 for ; Fri, 26 Nov 2010 07:49:23 -0400 (AST) Received: from unipex-local-server (unipex.wipex [172.16.20.247]) by smtp.go-wipex.it (Postfix) with ESMTP id 70E3810B00B7 for ; Fri, 26 Nov 2010 12:39:25 +0100 (CET) Received: from [192.168.1.103] (michele [192.168.1.103]) by unipex-local-server (Postfix) with ESMTP id E7FEB560001 for ; Fri, 26 Nov 2010 12:39:17 +0100 (CET) Message-ID: <4CEF9C66.30100@unipex.it> Date: Fri, 26 Nov 2010 12:39:18 +0100 From: Michele Petrazzo - Unipex User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1b1) Gecko/20060724 Thunderbird/2.0a1 Mnenhy/0.7.4.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: group by hour + distinct Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201011/67 X-Sequence-Number: 34805 Hi list, Into a table like this: id_user | my_date and some data inside 1 | 2010/11/25 00:01:00 1 | 2010/11/25 00:02:00 1 | 2010/11/25 01:01:00 2 | 2010/11/25 02:01:00 3 | 2010/11/25 02:01:00 3 | 2010/11/25 02:06:00 1 | 2010/11/25 03:01:00 I'm looking for a query that say me, hour per hour, how many unique id_user are inside that range. With the simple data above, I'm looking for: hour | count 0 | 1 1 | 1 2 | 2 3 | 1 Like now, with my tests, I achieve only a hour | count 0 | 2 1 | 1 2 | 3 3 | 1 My real query and data: SELECT count(id_user) from some_table where my_date >= '2010/11/25 00:00:00' and my_date < '2010/11/25 01:00:00'; count ------- 90 (1 row) SELECT distinct(id_user) from some_table where my_date >= '2010/11/25 00:00:00' and my_date < '2010/11/25 01:00:00' order by id_user; Give me 69 rows, that are the real unique id_user that I have and I'm looking for. One of a query that I use without success: SELECT count(distinct some_table.my_date), EXTRACT(hour from my_date) as h from some_table where my_date >= '2010/11/25' and my_date < '2010/11/26' group by h order by h; count | h -------+---- 90 | 0 63 | 1 ... and so on Someone? Thanks, Michele