Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 8A2EA1014388 for ; Thu, 1 Mar 2012 05:30:44 -0400 (AST) Received: from mail-wi0-f174.google.com ([209.85.212.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S32LS-0007jI-5f for pgsql-sql@postgresql.org; Thu, 01 Mar 2012 09:30:44 +0000 Received: by wibhm2 with SMTP id hm2so170321wib.19 for ; Thu, 01 Mar 2012 01:30:29 -0800 (PST) Received-SPF: pass (google.com: domain of couling@gmail.com designates 10.216.135.66 as permitted sender) client-ip=10.216.135.66; Authentication-Results: mr.google.com; spf=pass (google.com: domain of couling@gmail.com designates 10.216.135.66 as permitted sender) smtp.mail=couling@gmail.com; dkim=pass header.i=couling@gmail.com Received: from mr.google.com ([10.216.135.66]) by 10.216.135.66 with SMTP id t44mr2093780wei.52.1330594228997 (num_hops = 1); Thu, 01 Mar 2012 01:30:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=4lafzAoXKYM3vnTAlb7aTp4cQmgc06q986aSu0IbkbU=; b=efarqlKw3XYicYQKOlErSlU3YOxNd5cPO2fOQ/kVMgfivAEqO+Mhkhs6pOR1TBpzXO vo0CKYqr+i4Qi/RQ46/VyTfAoDufPv4ztvksbjcbHmPHQKujzDUdNaGevR2j4MRFLPx1 sFzLQKUEPubk+emZpifuMrjuT8Y7H3Q6HE1b4= Received: by 10.216.135.66 with SMTP id t44mr1679373wei.52.1330594228924; Thu, 01 Mar 2012 01:30:28 -0800 (PST) Received: from zbox.pedal.me.uk (cpc2-king5-0-0-cust1226.perr.cable.virginmedia.com. [82.36.220.203]) by mx.google.com with ESMTPS id fl2sm7033579wib.4.2012.03.01.01.30.26 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 01 Mar 2012 01:30:27 -0800 (PST) Received: from [192.168.104.172] (unknown [77.107.188.229]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by zbox.pedal.me.uk (Postfix) with ESMTPSA id 495C91980D5C; Thu, 1 Mar 2012 09:30:27 +0000 (GMT) Message-ID: <4F4F41AD.4060408@pedal.me.uk> Date: Thu, 01 Mar 2012 09:30:21 +0000 From: Philip Couling User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 MIME-Version: 1.0 To: reto.buchli@wsl.ch CC: pgsql-sql@postgresql.org Subject: Re: No sort with except References: <4F4F3075.3030506@frank.uvena.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201203/4 X-Sequence-Number: 36399 Hi Reto You are right to assume that you're query is ordering the second select and not the whole query. To order the query as a whole it in parentheses and put the ORDER BY at the end: ( SELECT foo FROM X EXCEPT SELECT foo FROM Y ) ORDER BY foo; Hope this helps On 01/03/2012 08:56, reto.buchli@wsl.ch wrote: > > pgsql-sql-owner@postgresql.org schrieb am 01.03.2012 09:16:53: > >> From: Frank Lanitz >> To: pgsql-sql@postgresql.org, >> Date: 01.03.2012 09:16 >> Subject: Re: [SQL] No sort with except >> Sent by: pgsql-sql-owner@postgresql.org >> >> Am 01.03.2012 09:13, schrieb reto.buchli@wsl.ch: >> > Dear all, >> > >> > When I run the following SQL with PostgreSQL 9.1: >> > >> > -- >> > SELECT DISTINCT ON (pernr) pernr, vorna, nachn, eindt, ausdt, updat, > status >> > FROM person >> > >> > WHERE eindt <= TO_CHAR(CURRENT_DATE,'YYYYMMDD') >> > AND ausdt >= TO_CHAR(CURRENT_DATE,'YYYYMMDD') >> > >> > ORDER BY pernr, eindt DESC; >> > -- >> > >> > it works. I get the most recent persons, even if one came back within >> > this time range. >> > >> > But if i do this: >> > >> > --- >> > SELECT DISTINCT ON (pernr) pernr, vorna, nachn, eindt, ausdt, updat, > status >> > FROM person >> > >> > WHERE eindt <= TO_CHAR(CURRENT_DATE,'YYYYMMDD') >> > AND ausdt >= TO_CHAR(CURRENT_DATE,'YYYYMMDD') >> > EXCEPT >> > >> > SELECT DISTINCT ON (pernr) pernr, vorna, nachn, eindt, ausdt, updat, >> > status >> > FROM person >> > RIGHT JOIN table ON sobid = CAST (pernr AS VARCHAR) AND objid > = 10 >> > WHERE eindt <= TO_CHAR(CURRENT_DATE,'YYYYMMDD') >> > AND ausdt >= TO_CHAR(CURRENT_DATE,'YYYYMMDD') >> > ORDER BY pernr, eindt DESC; >> > --- >> > >> > In this case the ORDER BY does not work: I will get the same person >> > data, either with DESC as with ASC, even when this should change. >> > >> > Does anyone have an explanation for this? >> >> >> Don't you sort just the part at EXCEPT? >> >> Cheers, >> Frank >> >> > Hi Frank > This may be. But as I understand, this will sort the result set. I'm > also not able to place ORDER BY before the EXCEPT. > > Am I wrong? > > Cheers, > Reto