Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 5AC84632555 for ; Sun, 4 Oct 2009 01:07:56 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 72031-09 for ; Sun, 4 Oct 2009 04:07:45 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by mail.postgresql.org (Postfix) with SMTP id 6562663239A for ; Sun, 4 Oct 2009 01:07:45 -0300 (ADT) Received: (qmail invoked by alias); 04 Oct 2009 04:07:42 -0000 Received: from i59F794B7.versanet.de (EHLO [192.168.1.113]) [89.247.148.183] by mail.gmx.net (mp013) with SMTP; 04 Oct 2009 06:07:42 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX18EImtqRb8E5lRkYRVnIRL2FkL8I6f2Y0lFwYcAj5 1PZbg3pd4E/SI5 Message-ID: <4AC81FB2.3050600@gmx.net> Date: Sun, 04 Oct 2009 06:08:18 +0200 From: Andreas User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Need even more magic. Now for tricky counts. Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.67 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/16 X-Sequence-Number: 33215 Hi, there is a vast log-table that collects several state data for objects. (log_id, project_fk, object_fk, state_fk, log_type_fk, created_on::timestamp, .......) log_id is a sequence, project_fk foreign key on a project-table object_fk foreign key on a object-table state_fk can have 10 values 0, 10, 20, 30, ... log_type_fk describes the event that caused the entry I need counts of states of objects for a project starting at a given date t0 in 14 days distances. Because I need a row for every reporting day, I started out by creating a view that selects the relevant project_fk and only those log_type_fk that MIGHT be relevant. state_fk = 0 is irrelevant, too. The same view does a case when ... for every state_fk so that I can add them up later to get a cross-table. e.g. case when state_fk = 10 then 1 else 0 end as sate_10, case when state_fk = 20 then 1 else 0 end as sate_20, ... Then the view adds a integer-column period_nr that represents the nr of 14 day periods since t0. In the first 14 days have period_nr = 0, in the second 14 days it is 1 and so on. Now I need a query that calculates the sum for every column state_10, state_20, ..., state_90 from t0 to the current period_nr. t0 until t0 + 1 * 14 days ===> count(state_10), count(state_20), count(state_30) ... t0 until t0 + 2 * 14 days ... This would be nice. I'd be glad if you could hint me up to here. Even nicer would be a solution that adds just the last occurance for every object_fk within the current t0 - period. e.g. object_fk = 42 might appear in period 1 with state 50 in period 3 twice with state 40 and 20 The report should count it in period 1+2 as 50 and in period 3 and further just 1 time as 20 until the object gets logged again. This might prove to be a wee bit tricky.