Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id DB931632749 for ; Wed, 4 Aug 2010 08:48:18 -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 51149-09 for ; Wed, 4 Aug 2010 11:48:10 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.187]) by mail.postgresql.org (Postfix) with ESMTP id 738CF632712 for ; Wed, 4 Aug 2010 08:48:11 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0MVasZ-1OSYKc1ND5-00YzW4; Wed, 04 Aug 2010 13:48:09 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1OgcS8-0000ME-00 for pgsql-sql@postgresql.org; Wed, 04 Aug 2010 13:48:08 +0200 Date: Wed, 4 Aug 2010 13:48:08 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Help Need some hindsight Message-ID: <20100804114808.GB5435@a-kretschmer.de> References: <4C5785D7.8040907@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4C5785D7.8040907@gmx.net> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V02:K0:iM1K3j1r3JgyNKWPwGjdKrvHSxs+56M5aCeAPtfCxWH UP2rmQ0mUEI32MJV9ic1cpDPJ3eGYIKXekBhkPVAcyhTOaA4oU AGKCVrRPJBC7XGPgIN+CbOiGLUqkYXac93FKvxhH3szm8EntO/ tnS9Qs4/noDTwnTzTOZzzM9TVcyr/31bllYreFyckX2aMvLATw S0UEMXSHwUC942wwW9kvg== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.81 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001, T_FRT_CONTACT=0.01 X-Spam-Level: X-Archive-Number: 201008/13 X-Sequence-Number: 34453 In response to Andreas : > Hi, > I need to display log events (again). > The log is simply like this > log ( log_id serial primary key, create_ts timestamp default > localtimestamp, object_id, state_id, ....... ) > > It records the state of objects and when and what happend to to change > this state. > > I'd like to get a list that shows the current state at any point of time > and the state of the last event before regarding the current object_id. > The tricky bit is that both states should appear in the same row for > every row. > > Help? something like: test=*# select * from log; id | ts | object_id | state_id ----+----------------------------+-----------+---------- 1 | 2010-08-04 13:24:19.648437 | 1 | 1 2 | 2010-08-04 13:24:26.957629 | 1 | 2 3 | 2010-08-04 13:24:38.883519 | 1 | 3 4 | 2010-08-04 13:24:43.60719 | 1 | 2 5 | 2010-08-04 13:24:51.123276 | 1 | 4 (5 rows) test=*# test=*# test=*# select max(case when row_number=1 then id else null end) as current_state_id, max(case when row_number=2 then id else null end) as last_state_id, max(case when row_number=1 then state_id else null end) as current_state, max(case when row_number=2 then state_id else null end) as last_state from ((select 1 as row_number, * from log where object_id=1 and ts < '2010-08-04 13:24:38.883520' order by ts desc limit 1) union all (select 2, * fromlog where object_id=1 and ts < '2010-08-04 13:24:38.883520' order by ts desc limit 1 offset 1)) foo ; current_state_id | last_state_id | current_state | last_state ------------------+---------------+---------------+------------ 3 | 2 | 3 | 2 (1 row) As you can see, the where-condition contains a timestamp that isn't in the table, it's after the id=3. Maybe there are other, better solutions... HTH, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99