agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-sql@lists.postgresql.org
Subject: Re: Sort a table by a column value that is a column name?
Date: Fri, 9 Jul 2021 10:32:21 -0600
Message-ID: <727312db-517f-8a27-49c2-1b452ba8fee2@gmail.com> (raw)
In-Reply-To: <1fe4b9c57fe60ce5e0bdd271c4f7924c3587b775.camel@recarea.com>
References: <1fe4b9c57fe60ce5e0bdd271c4f7924c3587b775.camel@recarea.com>

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)

view thread (5+ messages)  latest in thread

Message-ID: <727312db-517f-8a27-49c2-1b452ba8fee2@gmail.com>
Permalink:  ../727312db-517f-8a27-49c2-1b452ba8fee2@gmail.com/
Also on:    postgresql.org/message-id/727312db-517f-8a27-49c2-1b452ba8fee2@gmail.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: robjsargent@gmail.com, pgsql-sql@lists.postgresql.org
  Subject: Re: Sort a table by a column value that is a column name?
  In-Reply-To: <727312db-517f-8a27-49c2-1b452ba8fee2@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox