Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1tQS-00075X-HE for pgsql-sql@arkaria.postgresql.org; Fri, 09 Jul 2021 16:32:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m1tQR-0007uC-Fc for pgsql-sql@arkaria.postgresql.org; Fri, 09 Jul 2021 16:32:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1tQR-0007u3-5o for pgsql-sql@lists.postgresql.org; Fri, 09 Jul 2021 16:32:27 +0000 Received: from mail-ot1-x335.google.com ([2607:f8b0:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m1tQP-000490-5P for pgsql-sql@lists.postgresql.org; Fri, 09 Jul 2021 16:32:26 +0000 Received: by mail-ot1-x335.google.com with SMTP id z18-20020a9d7a520000b02904b28bda1885so8545244otm.7 for ; Fri, 09 Jul 2021 09:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=hbGYZUzWk+8KKLp4C+whxGwTdi3X6vpBph5P/r/dalM=; b=f/XEEw2ysjG2IYh/JtPoGBEV/NEGCV5/boUuaIbwDULkl7kS3wGw6ybaU/2C9AONt8 34EGuq1tnCpGdIQLKbKYCL4ZO4dCKZ3BBXVEmHIqUJYHOuBx/m0yGQ6QwPyNhSRNBkwM eypufbQg4SPMtXBUbixvW3E4FCtvXawkFA2+/nP6TY3l0ztH+J+bhKYpmrDZiO/arXjb 8xOiYrcIfZxcECBr7gon1vXu5iMO9BHXio9aaamydU0D5XJjpi0nO/UFqLvI1DrgrmJg 2nCX2lF3ngYI1Ja/NouD3LRKrtmbzlOuqi07tKFStUp6mMCfliYwlgdfNtmR3SCZ0Q31 oNFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=hbGYZUzWk+8KKLp4C+whxGwTdi3X6vpBph5P/r/dalM=; b=lEINVYRy/FryuBzBKOnfwEtoYRdzETcte2ugKOy+YOjH2Qu9eGO8vNJLml9Y5fOJAt X84s/eWvW71V8YpCCUhw7vPa0jgetL542cGP1ptyVa7IFjHNu46IaYOoKXAVYTsuIbKQ +eZIslF8zyMpYXK1j5y32bJ58NZatxkZ31RlS0vkFmc9FQfxD2L2yoCfHsXXYW2iO1o6 KVnc3BB6L+sqWKjJLPv063yv2WjRMKG0bqiBgn8p2n8U2Fm/G/fG7PxwPetBnLG2X//8 SBswzgmpQNaj843jieOdEV7ginBQCoZzZpM+gTU3FGS8QQuiOmj7tcHGSmiSVMcyyvoH GCmQ== X-Gm-Message-State: AOAM530bxES+2pR44f2zSB0JWW50rSZ+WhQYejR8pdJtsb7SkGodfpRJ LXEjNsnKIYLnDWkwUzJY3J4vOdi8lvU= X-Google-Smtp-Source: ABdhPJzlMNsyw5uONAbsMWxB7yEs7WzjMrmJYjfUMupbf4gWOeUofdsZ34SHO/G6+zOkzps5lT6Cpg== X-Received: by 2002:a05:6830:308c:: with SMTP id f12mr16300088ots.337.1625848343376; Fri, 09 Jul 2021 09:32:23 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:b811:411:db94:8721? ([2601:681:5500:dde0:b811:411:db94:8721]) by smtp.gmail.com with ESMTPSA id k25sm1087819ood.45.2021.07.09.09.32.22 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 09 Jul 2021 09:32:23 -0700 (PDT) Subject: Re: Sort a table by a column value that is a column name? To: pgsql-sql@lists.postgresql.org References: <1fe4b9c57fe60ce5e0bdd271c4f7924c3587b775.camel@recarea.com> From: Rob Sargent Message-ID: <727312db-517f-8a27-49c2-1b452ba8fee2@gmail.com> Date: Fri, 9 Jul 2021 10:32:21 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <1fe4b9c57fe60ce5e0bdd271c4f7924c3587b775.camel@recarea.com> Content-Type: multipart/alternative; boundary="------------0B08360B72C34F6AB4EBEB68" Content-Language: en-CA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------0B08360B72C34F6AB4EBEB68 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 7/9/21 10:59 AM, overland wrote: > I'm writing a program and I'm aiming to seperate logic from the database and at the same time optimize program performance. I was doing a sort on Postgresql 13 query results in a program but pushing > the sort to postgresql would optimize performance. So I modified an existing query to do the sorting now and it isn't sorting as I want, but I don't know what to expect. I'm looking to sort a table > using a column name that is stored in another table. I don't know the column to sort on when the query is written. > > An example is below that is quick and dirty and shows what I'm trying to do. There isn't an error when the query is executed, yet the sort doesn't work and fails sighlently. Is there another way to > accomplish the same thing? > > > > > > CREATE TABLE list ( > id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, > attribute TEXT, > property TEXT, > descid INT > ); > > > CREATE TABLE descriptor ( > id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, > name TEXT > ); > > INSERT INTO descriptor(name) VALUES('attribute'), ('property'); > INSERT INTO list(attribute, property, descid) VALUES('todo', 'camping', 1); > INSERT INTO list(attribute, property, descid) VALUES('hooplah', 'glamping', 1); > INSERT INTO list(attribute, property, descid) VALUES('stuff', 'other', 1); > INSERT INTO list(attribute, property, descid) VALUES('car', 'bike', 2); > INSERT INTO list(attribute, property, descid) VALUES('cat', 'hat', 2); > INSERT INTO list(attribute, property, descid) VALUES('bat', 'that', 2); > > > > > SELECT attribute, property, descid > FROM list AS l > JOIN descriptor AS d ON l.descid = d.id > WHERE l.id < 4 > ORDER BY name; > > > And I take it your having trouble with "name". Keep in mind that one can sort by values not in the select criteria but that won't help the client much. I think you'll have to do a case analysis on the available values of descriptor.text (which I suspect the client is already doing) and formulate the request based on the clients choice of, in this case, either "attribute" or "property".  You can either prepare a map of all possible statements or generate the sql on-demand (or maybe generate the prepared statements as needed and populate the map) --------------0B08360B72C34F6AB4EBEB68 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 7/9/21 10:59 AM, overland wrote:
I'm writing a program and I'm aiming to seperate logic from the database and at the same time optimize program performance. I was doing a sort on Postgresql 13 query results in a program but pushing
the sort to postgresql would optimize performance. So I modified an existing query to do the sorting now and it isn't sorting as I want, but I don't know what to expect. I'm looking to sort a table
using a column name that is stored in another table. I don't know the column to sort on when the query is written.

An example is below that is quick and dirty and shows what I'm trying to do. There isn't an error when the query is executed, yet the sort doesn't work and fails sighlently. Is there another way to
accomplish the same thing?





CREATE TABLE list (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    attribute TEXT,
    property TEXT,
    descid INT
);


CREATE TABLE descriptor (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name TEXT
);

INSERT INTO descriptor(name) VALUES('attribute'), ('property');   
INSERT INTO list(attribute, property, descid) VALUES('todo', 'camping', 1);
INSERT INTO list(attribute, property, descid) VALUES('hooplah', 'glamping', 1);
INSERT INTO list(attribute, property, descid) VALUES('stuff', 'other', 1);
INSERT INTO list(attribute, property, descid) VALUES('car', 'bike', 2);
INSERT INTO list(attribute, property, descid) VALUES('cat', 'hat', 2);
INSERT INTO list(attribute, property, descid) VALUES('bat', 'that', 2);




SELECT attribute, property, descid
FROM list AS l
JOIN descriptor AS d ON l.descid = d.id
WHERE l.id < 4
ORDER BY name;



And I take it your having trouble with "name".

Keep in mind that one can sort by values not in the select criteria but that won't help the client much. 

I think you'll have to do a case analysis on the available values of descriptor.text (which I suspect the client is already doing) and formulate the request based on the clients choice of, in this case, either "attribute" or "property".  You can either prepare a map of all possible statements or generate the sql on-demand (or maybe generate the prepared statements as needed and populate the map)
--------------0B08360B72C34F6AB4EBEB68--