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 1m1sxe-000633-LK for pgsql-sql@arkaria.postgresql.org; Fri, 09 Jul 2021 16:02:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m1sxd-0002w5-Gr for pgsql-sql@arkaria.postgresql.org; Fri, 09 Jul 2021 16:02:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1sxd-0002vx-1N for pgsql-sql@lists.postgresql.org; Fri, 09 Jul 2021 16:02:41 +0000 Received: from se15-2.privateemail.com ([198.54.127.73]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m1sxb-0001xO-0m for pgsql-sql@lists.postgresql.org; Fri, 09 Jul 2021 16:02:40 +0000 Received: from new-01-3.privateemail.com ([198.54.122.47]) by se15.registrar-servers.com with esmtpsa (TLSv1.2:AES128-GCM-SHA256:128) (Exim 4.92) (envelope-from ) id 1m1sxU-0005xB-Am for pgsql-sql@lists.postgresql.org; Fri, 09 Jul 2021 09:02:35 -0700 Received: from MTA-13.privateemail.com (unknown [10.50.14.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by NEW-01-3.privateemail.com (Postfix) with ESMTPS id B44EFA9A for ; Fri, 9 Jul 2021 12:01:19 -0400 (EDT) Received: from mta-13.privateemail.com (localhost [127.0.0.1]) by mta-13.privateemail.com (Postfix) with ESMTP id 8C4F318002DB for ; Fri, 9 Jul 2021 12:01:19 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=recarea.com; s=default; t=1625846479; bh=62TL3BTvnoRamwNOOSdl0cQMIV9dg4xbOmzX8BZA+uM=; h=Subject:From:To:Date:From; b=Q/wxen2i/6XKc2pGsRvzmRH025CqJj1NE4zsfG5mIMt2wDk5dyuEWxLpVlofB4l+Q FrQPdQ8tjLI9psMyP9ugFLvYv6jtkc/0/PDEKXQMq2Ytbe2X0p1V26kTxZ0G579fry ixkapssQzu06t1qP6orjfpQkAy8ISIn2YLiVE29n64UbijCvk+IUyOzIZ1LXUMDfqk DX//grLvYm8V1kDZMJoap6q8/YIWthJVayvtgwT3zsD9jEj0VC6QTS4nygMHoLOyc4 N6lseBoyQrgVfjCLgNWziOvTrNHlw4ETfR4cvbvwnaSHsZyVWUkNi7o4w79IOpmqql VwiGx4FP/5xIQ== Received: from debian (unknown [10.20.151.210]) by mta-13.privateemail.com (Postfix) with ESMTPA id 235FB18002C7 for ; Fri, 9 Jul 2021 12:01:19 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=recarea.com; s=default; t=1625846479; bh=62TL3BTvnoRamwNOOSdl0cQMIV9dg4xbOmzX8BZA+uM=; h=Subject:From:To:Date:From; b=Q/wxen2i/6XKc2pGsRvzmRH025CqJj1NE4zsfG5mIMt2wDk5dyuEWxLpVlofB4l+Q FrQPdQ8tjLI9psMyP9ugFLvYv6jtkc/0/PDEKXQMq2Ytbe2X0p1V26kTxZ0G579fry ixkapssQzu06t1qP6orjfpQkAy8ISIn2YLiVE29n64UbijCvk+IUyOzIZ1LXUMDfqk DX//grLvYm8V1kDZMJoap6q8/YIWthJVayvtgwT3zsD9jEj0VC6QTS4nygMHoLOyc4 N6lseBoyQrgVfjCLgNWziOvTrNHlw4ETfR4cvbvwnaSHsZyVWUkNi7o4w79IOpmqql VwiGx4FP/5xIQ== Message-ID: <1fe4b9c57fe60ce5e0bdd271c4f7924c3587b775.camel@recarea.com> Subject: Sort a table by a column value that is a column name? From: overland To: pgsql-sql@lists.postgresql.org Date: Fri, 09 Jul 2021 09:59:25 -0700 Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5-1.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV using ClamSMTP X-Originating-IP: 198.54.122.47 X-SpamExperts-Domain: o3.privateemail.com X-SpamExperts-Username: out-03 Authentication-Results: registrar-servers.com; auth=pass (plain) smtp.auth=out-03@o3.privateemail.com X-SpamExperts-Outgoing-Class: ham X-SpamExperts-Outgoing-Evidence: Combined (0.15) X-Recommended-Action: accept X-Filter-ID: Pt3MvcO5N4iKaDQ5O6lkdGlMVN6RH8bjRMzItlySaT9WLQux0N3HQm8ltz8rnu+BPUtbdvnXkggZ 3YnVId/Y5jcf0yeVQAvfjHznO7+bT5xxcBle2ww0+fg0m5Qgd/NfR+YkeEyuzsEMHBSE6yC1w32M 30F3H6pq2t3ZqB0uMoLilMVfw4P4qkZAhbJeHV2nVrkVq5D4oaud2KgHdRDTavqCSlNRosAtFUiy dviZngS/Sp9wjFPVK76LTNeY3PPIJDyGFXqXClYAZ2r7cPFe/1LkvQbf9vDc1KU//y47OQyDwIE7 VKe+bqpcdCns72R1Otd4AF6jNt/hlAxzLZLBFaqK0ccu7v5lR8tBuDZkmKacmuBJwe36CN1gwFhC KkEevvsTbUnBm4eP/gSAGzl2380ayHS6fwMJkHHsvoTylavXZ390tTXDb91Nnaf1Gz774hJ877w3 BrOappvVwxNqXWVmZiDFiRtg8zYnq8FneOb9qNV/dgp+FFPSSde3UJEu4sw4+FXyxmIVea1OKVqa VtQtR9nvbSthSFIhHqku3g85Ix7/C9VwqPH/5QpQnZ0BoT+6/6ngnC3zT5MJtpTZ7L69ywUfK1GF yeI4etqXx2iqrJJ80NVlREkpW4CenvdvASJFC/49WOPBr5nlEUI4xLRHve4p4CvPNI1g05P3BJcS HQXS+Mq90YbKbRNbSlb5XEYGaeJlsB/ViaTurLCoMC4CcfbyHBdbhIyMvb6Cqw450KcSyZEhOAQ2 cLnVzsatlKJ7jrNRXsNt/cBtRLSZF8d7yLncQa2bIVOuReCLAaEpoSFUjunl4IExMPRiniVcgtQC PWt4q24n88ps0GF/j5QHrTSvUcy4WVRHBD8TlliwJcBRp86ShlCM4G4PM7ysPiZWzeQ3U64OFDzo FfZlNqNzDthuNljSbTIkAdLiyXQ3ClTXlSfQV8rvfnHgqk4UkN5mJJCm7Jvx803pgHh0VZHr+ABt 46DVstjvEVECsgC2UYC3nHd+F0bSxeRmZK2YtnjPfkqpYEwdV9Z5txu5wx9pSvduGbjHw0gypj6o we7VYU8AA8eJSmRS8V9ZbwkKQVODEisIcaXJOhirP2+Z5XBqz7TRiWF91U/smddrJvE= X-Report-Abuse-To: spam@se5.registrar-servers.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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;