Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id C0B86B5DCE2 for ; Wed, 2 Nov 2011 13:51:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 48933-05 for ; Wed, 2 Nov 2011 16:51:27 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mailout01.ims-firmen.de (mailout01.ims-firmen.de [213.174.32.96]) by mail.postgresql.org (Postfix) with ESMTP id 1F9AAB5DCDF for ; Wed, 2 Nov 2011 13:51:26 -0300 (ADT) Received: from mailin03.ims-firmen.de ([192.168.1.112]) by mailout01.ims-firmen.de with esmtp (envelope-from ) id 1RLe28-0003OT-E4 for pgsql-sql@postgresql.org; Wed, 02 Nov 2011 17:51:24 +0100 Received: from [213.174.32.254] (helo=a-kretschmer.de) by mailin03.ims-firmen.de with esmtpsa (TLSv1:AES256-SHA:256) (envelope-from ) id 1RLe28-0007bF-7q for pgsql-sql@postgresql.org; Wed, 02 Nov 2011 17:51:24 +0100 Received: from kretschmer by a-kretschmer.de with local (Exim 4.69) (envelope-from ) id 1RLe27-00066e-0U for pgsql-sql@postgresql.org; Wed, 02 Nov 2011 17:51:23 +0100 Date: Wed, 2 Nov 2011 17:51:23 +0100 From: Andreas Kretschmer To: pgsql-sql@postgresql.org Subject: Re: Number timestamped rows Message-ID: <20111102165122.GA22934@tux> References: <20111102163619.135780@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20111102163619.135780@gmx.net> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: Mutt/1.5.18 (2008-05-17) 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, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201111/4 X-Sequence-Number: 35977 Jan Peters wrote: > Dear all, > maybe a stupid question, but: I have a table that is ordered like this: > > user_id|timestamp|event > 1 |0:1 |event_a > 1 |0:2 |event_b > 2 |0:1 |event_b > 2 |0:3 |event_c > 2 |0:4 |event_b > 3 |0:1 |event_a > > and I would like to number them according to their timestamps like this: > > user_id|timestamp|event |order > 1 |0:1 |event_a |1 > 1 |0:2 |event_b |2 > 2 |0:1 |event_b |1 > 2 |0:3 |event_c |2 > 2 |0:4 |event_b |3 > 3 |0:1 |event_a |1 > untested: select user_id, timestamp, event, row_number() over (partition by user_id order by timestamp) as "order" from your_table > How would I do this with an UPDATE statement (e.g.) in pgsql? I think, you don't need an UPDATE, just a SELECT. Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknown) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°