Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id E6361632886 for ; Thu, 8 Apr 2010 16:51:24 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 90938-01 for ; Thu, 8 Apr 2010 19:51:14 +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 02C50632224 for ; Thu, 8 Apr 2010 16:51:13 -0300 (ADT) Received: (qmail invoked by alias); 08 Apr 2010 19:51:11 -0000 Received: from mue-88-130-36-061.dsl.tropolys.de (EHLO [192.168.1.113]) [88.130.36.61] by mail.gmx.net (mp040) with SMTP; 08 Apr 2010 21:51:11 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX1/m0lpBmjbmnhhG5pmIm5wJj0KdDf5Ws2ACRpjJaV WU+86rzmxK0aEI Message-ID: <4BBE33B0.1050408@gmx.net> Date: Thu, 08 Apr 2010 21:51:12 +0200 From: Andreas User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Howto get a group_number like row_number for groups Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.62 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201004/32 X-Sequence-Number: 33959 Hi I'd like to have an alternating colorindex in the output of a query that consecutive rows that are the same within a colum the same number. The query generates a readable output from a log-table and a few others that hold referenced texts. log (log_id int, log_event_fk int, object_fk int , ts timestamp) events (event_id int, event text) objects (object_id int, object text, is_active bool) the easy part :) mind the the ordering is not primarily based on the timestamp select log_id, event, object, ts from log join events on event_id = log_event_fk join objects on object_id = object_fk where object.is_active order by object, ts Now I'd need a dynamically generated column that alternates between 0 and 1 so that I can later color the rows where object is the same. row_number() over (order by object, ts) % 2 or rank() over (order by object, ts) % 2 produces the 0/1 alternation for rows When I create a subselect for objects that adds the colorindex and join this to the log instead of objects, I get the group-color only if I omit the sorting on the timestamp. When I order the outer select by object, ts the colorindex gets 0 in every row. :( I'd like to get something as this 3, up, dev3, 2010-4-2 10:00, 0 8, down, dev3, 2010-4-2 14:00, 0 9, down, dev3, 2010-4-2 15:00, 0 1, up, dev7, 2010-4-2 09:00, 1 5, down, dev7, 2010-4-2 17:00, 1 2, up, dev11, 2010-4-2 12:00, 0 7, down, dev11, 2010-4-2 13:00, 0 . . regards :)