Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gRN34-0007Zw-AK for pgsql-sql@arkaria.postgresql.org; Mon, 26 Nov 2018 20:00:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gRN33-0003Bf-4c for pgsql-sql@arkaria.postgresql.org; Mon, 26 Nov 2018 20:00:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gRN32-0003BY-Tu for pgsql-sql@lists.postgresql.org; Mon, 26 Nov 2018 20:00:00 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gRN30-0000UT-Nm for pgsql-sql@lists.postgresql.org; Mon, 26 Nov 2018 20:00:00 +0000 Received: by mail-wr1-x42e.google.com with SMTP id x10so20283720wrs.8 for ; Mon, 26 Nov 2018 11:59:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:references:in-reply-to:subject:date:message-id :mime-version:content-transfer-encoding:thread-index :content-language; bh=WE/ew/zpdv9J9zIoN2nSTkorQT3lf/FMmedsWDhULVg=; b=HWxj+YLLto+UEdH7EMjeRUZHNs5ySb2EGNRLT+wiP2FjFAt+Htg07HBXADAEGZ4IEI gEN1tlRH3sdURqhJl/0OfKKLnn7DCIIbl7KNJO8nMbi9Ph9BxdOI1wfSvNk8FyrpgFWU NmoqZpmYNOpP7hdPveNJ/6UiXpxazP2XsCJBj11rGuhj/ulHc1x66J4xNhe1EHJOV7EL r/2diPJlh4LTyjbaN2KE3/QMULU5D4wZLOXUcrVW/4azPO/RE7R3U1C0u1lOqfWCF95R zZNEseEZIAxTvya5TSTuj5p+1UDUCxD9uJwNI1SDkjDb2wbA6WWd7Nhsmt+eiIy9ZtpX JRnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:references:in-reply-to:subject:date :message-id:mime-version:content-transfer-encoding:thread-index :content-language; bh=WE/ew/zpdv9J9zIoN2nSTkorQT3lf/FMmedsWDhULVg=; b=RMXHnVEzo3ORbzUWZGQMcDjmfrt9FdlfPCtN0QSfLcxR3i+c5g9rxFSkET3h/tcUwn LY95aoc6Vqev8CjsHdh4vYfhgO2llobkm23q1BfeBwclAKXb+qnzuK/auVbJ/eLY4GOY wWUqrzYtGhSxger6dACigc79y1WOSrtmYMy8Q7e11ReWCmRycdNG8Y87EIopfDk6edNo DLQFHd6nQJHGs3ADm4qhZyRmlFQuKAqIjNMu2ndhaHBb2W+UCS5S+1ZMQMOV1nAiXwLl RqP9DyQF7RgH4Ee+8vhb1ecoquKbXNfxtAn+E4wjn5P8K2rnc1aoxGZglTsYQtf4Y9zM Z+iQ== X-Gm-Message-State: AA+aEWaGOtACWf9AZcNPddz9pFD0f8Bidpq533HN42GamJyVgRQozWYe BOdoNcg96ZCVSM7EWuFs+TgHopB6 X-Google-Smtp-Source: AFSGD/VbxmOjZZ9nbwIxXHTNfNwvtzkTYDthxMRMhkzailXt4550jANWyLThVFYOWwqRYhEkXw+fyw== X-Received: by 2002:adf:b6a1:: with SMTP id j33mr24962821wre.55.1543262397651; Mon, 26 Nov 2018 11:59:57 -0800 (PST) Received: from LAPTOPE7EH7MDP (167.221-30-62.static.virginmediabusiness.co.uk. [62.30.221.167]) by smtp.gmail.com with ESMTPSA id h203sm434924wma.19.2018.11.26.11.59.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Nov 2018 11:59:57 -0800 (PST) From: "Mark Williams" To: "'David G. Johnston'" Cc: "'pgsql-sql'" References: <004301d485bc$00347250$009d56f0$@gmail.com> In-Reply-To: Subject: RE: Select Distinct Order By Array_Position Date: Mon, 26 Nov 2018 19:59:55 -0000 Message-ID: <005301d485c2$9a6f1db0$cf4d5910$@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQEpSAIENSM0BdolJjQVlTJ5xV69VAG9YTmspqsKIcA= Content-Language: en-gb List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David, thanks. It was a bad example on my part. Could just as well have = been: (26194, 189, 2345). __ -----Original Message----- From: David G. Johnston =20 Sent: 26 November 2018 19:46 To: Mark Williams Cc: pgsql-sql Subject: Re: Select Distinct Order By Array_Position On Mon, Nov 26, 2018 at 12:12 PM Mark Williams = wrote: > I am ordering by documents.id and it appears in my select list. [...] > SELECT DISTINCT documents.id, page_no FROM texts LEFT JOIN documents=20 > on documents.id=3Dtexts.doc_id WHERE doc_id IN (26194, 2345, 189) AND = > (text LIKE '%RIVER%') ORDER BY array_position(ARRAY[26194, 2345,=20 > 189]::INTEGER[], documents.id) No, you are not ordering by documents.id, you are ordering by an = expression into which you are passing the documents.id value as one of = its components. When you use ORDER BY and DISTINCT together you basically are = short-handing: SELECT sq.* FROM (SELECT DISTINCT ...) AS sq ORDER BY sq.? If you want to order by something you have to include it exactly in the = select-list of the inner/distinct query. In this example, though, you could just "ORDER BY documents.id DESC"... David J.