Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 6FB611337B77 for ; Fri, 26 Nov 2010 08:19:15 -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 55738-08 for ; Fri, 26 Nov 2010 12:19:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from jupiter.marktest.pt (delta.marktest.pt [80.251.174.116]) by mail.postgresql.org (Postfix) with ESMTP id 626BB1337B43 for ; Fri, 26 Nov 2010 08:19:06 -0400 (AST) Received: from Proteu (proteu.marktest.pt [10.61.90.54]) by jupiter.marktest.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id W4GBP9CK; Fri, 26 Nov 2010 12:19:06 -0000 Message-ID: From: "Oliveiros d'Azevedo Cristina" To: "Michele Petrazzo - Unipex" , References: <4CEF9C66.30100@unipex.it> Subject: Re: group by hour + distinct Date: Fri, 26 Nov 2010 12:19:06 -0000 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.011 tagged_above=-5 required=5 tests=BAYES_20=-0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201011/68 X-Sequence-Number: 34806 Howdy, Michelle, If you write something like this, SELECT hour , COUNT(id_user) as count FROM ( SELECT EXTRACT(hour from my_date) as hour, id_user FROM michelle GROUP BY EXTRACT(hour from my_date),id_user )subquery GROUP BY hour for each hour it will count the number of distinct user_id's there are . If I understood correctly what you need... Can you please test it and see if it is OK for your needs? With me, it worked on the sample data you provided Best, Oliver ----- Original Message ----- From: "Michele Petrazzo - Unipex" To: Sent: Friday, November 26, 2010 11:39 AM Subject: [SQL] group by hour + distinct > 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 > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql