Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 587D3B5DC67 for ; Fri, 16 Sep 2011 20:56:18 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 37049-04 for ; Fri, 16 Sep 2011 23:56:11 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 63536B5DC64 for ; Fri, 16 Sep 2011 20:56:10 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1R4iGO-0002uM-6i for pgsql-sql@postgresql.org; Sat, 17 Sep 2011 01:56:08 +0200 Received: from f054053076.adsl.alicedsl.de ([78.54.53.76]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 17 Sep 2011 01:56:08 +0200 Received: from tim by f054053076.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 17 Sep 2011 01:56:08 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-sql@postgresql.org To: pgsql-sql@postgresql.org From: Tim Landscheidt Subject: Re: Window function sort order help Date: Fri, 16 Sep 2011 23:55:57 +0000 Organization: Lines: 36 Message-ID: References: <25770AAABEA9A2499B5427E0DCEE9C9B323706179A@BE262.mail.lan> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: f054053076.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:6mamDLHsBuzWX9IOFbTBj/ruoCI= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/36 X-Sequence-Number: 35836 Dianna Harter wrote: > Having trouble getting a window function to sort correctly. > Given this data > consumer_id | move_date | history_timestamp > -------------+------------+---------------------------- > 12345| 2008-01-05 | 2007-12-11 06:02:26.842171 > 12345| 2008-02-29 | 2008-02-05 07:22:38.04067 > 12345| 2008-02-29 | 2008-07-11 09:03:42.44044 > 23456| 2009-01-01 | 2008-11-12 07:33:32.656658 <-- > 23456| 2009-01-28 | 2008-11-14 01:57:40.264335 > 23456| 2009-01-01 | 2008-12-04 17:14:20.279999 <-- > 23456| 2009-01-01 | 2008-12-31 00:33:37.204968 > 23456| 2009-01-01 | 2011-06-08 04:16:41.646521 > 34567| 2010-05-07 | 2010-06-08 05:14:43.842172 > I'm trying to get the timestamp when the consumer last changed their move_date. (Notice consumer_id 23456 set their move_date to 2009-01-01 then changed and then changed it back. In the end, I want the timestamp from when they changed it to 2009-01-01 the second time.) > My thought was to do an intermediary step to find the timestamp for each time it switched. From there I can grab the max(timestamp) for each consumer. > [...] > Any suggestions to get the order by to occur first then the partition by or maybe there another approach that I could use? If I understand the question correctly, try: | SELECT DISTINCT ON (consumer_id) consumer_id, move_date, history_timestamp | FROM (SELECT consumer_id, move_date, history_timestamp, | LAG(move_date) OVER (PARTITION BY consumer_id ORDER BY consumer_id, history_timestamp) AS previous_move_date | FROM consumer_hist) AS SubQuery | WHERE move_date IS DISTINCT FROM previous_move_date | ORDER BY consumer_id, history_timestamp DESC; Tim