Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 9AF13B5DBD8 for ; Wed, 14 Sep 2011 06:02:00 -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 90757-06 for ; Wed, 14 Sep 2011 09:01:54 +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 5C9E4B5DBD6 for ; Wed, 14 Sep 2011 06:01:53 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1R3lLs-00038B-J1 for pgsql-sql@postgresql.org; Wed, 14 Sep 2011 11:01:52 +0200 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 14 Sep 2011 11:01:52 +0200 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 14 Sep 2011 11:01:52 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: Window function sort order help Date: Wed, 14 Sep 2011 11:02:18 +0200 Lines: 32 Message-ID: References: <25770AAABEA9A2499B5427E0DCEE9C9B323706179A@BE262.mail.lan> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.229 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_NUMERIC_HELO=1.164, RP_MATCHES_RCVD=-0.504, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201109/29 X-Sequence-Number: 35829 Nicoletta Maia, 14.09.2011 10:30: > SELECT `X`.`consumer_id`, `X`.`move_date` , `X`.`history_timestamp` , > MIN( `Y`.`history_timestamp` ) AS `start_time` > FROM > `Table` AS `X` > JOIN > `Table` AS `Y` > ON `X`.`consumer_id` = `Y`.`consumer_id` > AND `X`.`move_date` = `Y`.`move_date` > AND `X`.`history_timestamp`>= `Y`.`history_timestamp` > WHERE NOT EXISTS ( > SELECT * > FROM `Table` AS `Z` > WHERE `X`.`consumer_id` = `Z`.`consumer_id` > AND `X`.`move_date`<> `Z`.`move_date` > AND `X`.`history_timestamp`>= `Z`.`history_timestamp` > AND `Y`.`history_timestamp`<= `Z`.`history_timestamp` > ) > GROUP BY `X`.`consumer_id`, `X`.`move_date` , `X`.`history_timestamp` > ORDER BY `X`.`consumer_id`, `X`.`history_timestamp` ASC > > With Y I select history_timestamp preceding the current row with the > same move_date. > With Z I verify that no changes have occurred to move_date between > X.history_timestamp and Y.history_timestamp. > That is not a valid PostgreSQL SQL statement. Postgres does not use "backticks" for quoting, it uses the standard double quotes. Thomas