Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UwAOF-0003mO-3v for pgsql-sql@arkaria.postgresql.org; Mon, 08 Jul 2013 12:17:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UwAOE-00057X-Ha for pgsql-sql@arkaria.postgresql.org; Mon, 08 Jul 2013 12:17:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UwAOD-00056M-Aa for pgsql-sql@postgresql.org; Mon, 08 Jul 2013 12:17:57 +0000 Received: from mout.gmx.net ([212.227.15.18]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UwAOA-0005Xv-CW for pgsql-sql@postgresql.org; Mon, 08 Jul 2013 12:17:57 +0000 Received: from [192.168.1.113] ([88.130.33.170]) by mail.gmx.com (mrgmx103) with ESMTPSA (Nemesis) id 0LgqEs-1URlF12EVC-00oBdZ for ; Mon, 08 Jul 2013 14:17:53 +0200 Message-ID: <51DAAE09.9090905@gmx.net> Date: Mon, 08 Jul 2013 14:18:17 +0200 From: Andreas User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: monthly statistics Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:rDqy/ZrGYpWFkkti1qr8wx7ZIzmZLnrkCMPB9tABrMHgLVHfc5b L1PqYw/XX+bP+G5gs83s23MyjW97bulxNAyH3UhAi4Iy8/JOKjifYEwQAGd5a5xqzjnfNfu C2YPGI96aB7h+nhDT9iXGZraHvefFbvD1tW8iSqJzbqSuvyZoFyOXCOv5hfrqjQNBWbui4n J7KgYAu2JNoqC4dQp/Fhg== X-Pg-Spam-Score: -2.2 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Hi, I need to show a moving statistic of states of objects for every month since beginning of 2013. There are tables like objects ( id integer, name text ); state ( id integer, state text ); 10=A, 20=B ... 60=F history ( object_id integer, state_id, ts timestamp ); Every event that changes the state of an object is recorded in the history table. I need to count the numbers of As, Bs, ... on the end of month. The subquery x finds the last state before a given date, here february 1st. select s.status, count(*) from ( select distinct on ( object_id ) status_id from history where ts < '2013/02/01' order by object_id, ts desc ) as x join status as s on x.status_id = s.id group by s.status order by s.status; Now I need this for a series of months. This would give me the relevant dates. select generate_series ( '2013/02/01'::date, current_date + interval '1 month', interval '1 month' ) How could I combine those 2 queries so that the date in query 1 would be replaced dynamically with the result of the series? To make it utterly perfect the final query should show a crosstab with the states as columns. It is possible that in some months not every state exists so in this case the crosstab-cell should show a 0. Month A B C ... 2013/02/01 2013/03/01 ... -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql