Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id AA9DDB5DC30 for ; Tue, 13 Sep 2011 08:53:47 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 41809-01 for ; Tue, 13 Sep 2011 11:52:01 +0000 (UTC) X-Greylist: delayed 00:26:19.199199 by SQLgrey-1.7.6 Received: from mailex.spillgroup.com (mailex.spillgroup.com [77.95.99.164]) by mail.postgresql.org (Postfix) with ESMTP id 5353DB5DBD6 for ; Tue, 13 Sep 2011 08:52:01 -0300 (ADT) Received: from ECHO.spil.local (echo.spil.local [192.168.92.115]) by mailex.spillgroup.com (Postfix) with ESMTP id 4F3F112C044 for ; Tue, 13 Sep 2011 13:25:40 +0200 (CEST) Received: from D116.spil.local (192.168.92.26) by ECHO.spil.local (192.168.92.100) with Microsoft SMTP Server (TLS) id 14.1.289.1; Tue, 13 Sep 2011 13:25:40 +0200 Message-ID: <4E6F3DB3.4030905@spilgames.com> Date: Tue, 13 Sep 2011 13:25:39 +0200 From: Guillaume Roger Organization: Spilgames User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0.2) Gecko/20110906 Thunderbird/6.0.2 MIME-Version: 1.0 To: Subject: Partition over a sliding date window 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: 201109/26 X-Sequence-Number: 35826 Hi, I have a simple table with 3 fields: date, value and username, which can hold identical rows. What I am trying to do is to have for each date the count of distinct username for the 30 previous days. I was hoping to get this with a partition, as in the following non-working exemple SELECT t.date , t.value , COUNT(DISTINCT t.username) OVER (PARTITION date BETWEEN t.date - INTERVAL '29 days' and t.date) FROM table t GROUP BY date , value , username ; There are many issues with this query: - distinct not implemented for window function - COUNT () OVER is not seen as an aggregate function, I thus need to add username in the GROUP BY clause, which leads to wrong result - I am not convinced that the date BETWEEN is valid either, but the other issues prevent me to check this. Is there a way to do what I am looking for with partitions, or should I just give up and use 'usual' sql? Thanks, Guillaume