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 1m1uxb-0001cP-7A for pgsql-sql@arkaria.postgresql.org; Fri, 09 Jul 2021 18:10:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m1uxa-00060a-3a for pgsql-sql@arkaria.postgresql.org; Fri, 09 Jul 2021 18:10:46 +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 1m1uxZ-00060L-Ke for pgsql-sql@lists.postgresql.org; Fri, 09 Jul 2021 18:10:45 +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 1m1uxX-00030j-0k for pgsql-sql@lists.postgresql.org; Fri, 09 Jul 2021 18:10:44 +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 1m1uxL-000BfN-Nh; Fri, 09 Jul 2021 11:10:41 -0700 Received: from MTA-15.privateemail.com (unknown [10.50.14.40]) (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 6C6D7A76; Fri, 9 Jul 2021 14:10:30 -0400 (EDT) Received: from mta-15.privateemail.com (localhost [127.0.0.1]) by mta-15.privateemail.com (Postfix) with ESMTP id 3AF7A18001A2; Fri, 9 Jul 2021 14:10:30 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=recarea.com; s=default; t=1625854230; bh=NF4XVB5ScG8fw28abs3bkDo0XxDfIukO6UInIM9K+aU=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=esZN2D20wG9zVrdki/KwZdixd4fDHeY99kYZxzFaICsMszNu7BkluocRjc7Y5J8g8 d7BCdR48KDwczXY7L49cPga8vMIs2DZ6s4ypRIzfIiY4ZH8DJpBKRgtpl234DYn7yp JG+OWOG9T8pmKFx2cIwaXg3WGs014OEZz0WAloV86AyHuUbeJUiLjI0iIPD1A7PLAb BQHUtxEhbXWEoQPL6PzoULzwdE/gjKF30f9dx7+XjzyjSjtFhg2uOHuh0R3rC6kRBg ubw4IcQ8HuRryWNgBHBgpXqMM3yu9RcIVXF0ugwbwdr2fyZdhTSxjkSwFsSHBqFmWg QL/HzVPa8flWw== Received: from debian (unknown [10.20.151.245]) by mta-15.privateemail.com (Postfix) with ESMTPA id A542318000A0; Fri, 9 Jul 2021 14:10:29 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=recarea.com; s=default; t=1625854230; bh=NF4XVB5ScG8fw28abs3bkDo0XxDfIukO6UInIM9K+aU=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=esZN2D20wG9zVrdki/KwZdixd4fDHeY99kYZxzFaICsMszNu7BkluocRjc7Y5J8g8 d7BCdR48KDwczXY7L49cPga8vMIs2DZ6s4ypRIzfIiY4ZH8DJpBKRgtpl234DYn7yp JG+OWOG9T8pmKFx2cIwaXg3WGs014OEZz0WAloV86AyHuUbeJUiLjI0iIPD1A7PLAb BQHUtxEhbXWEoQPL6PzoULzwdE/gjKF30f9dx7+XjzyjSjtFhg2uOHuh0R3rC6kRBg ubw4IcQ8HuRryWNgBHBgpXqMM3yu9RcIVXF0ugwbwdr2fyZdhTSxjkSwFsSHBqFmWg QL/HzVPa8flWw== Message-ID: <111951a2dfa543294b70ddfcb44b92e06fc1e2c2.camel@recarea.com> Subject: Re: Sort a table by a column value that is a column name? From: overland To: Steve Midgley Cc: pgsql-sql Date: Fri, 09 Jul 2021 12:08:35 -0700 In-Reply-To: References: <1fe4b9c57fe60ce5e0bdd271c4f7924c3587b775.camel@recarea.com> 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+bT5x+6PLpb0KuPdBVkzB0UpoOGIpECxRcWFqBXPoCdJ9EsW6Q VnJQ/8U30WvAJlekFoD5HjBbTcnmaR7mdxKugTMF4KN+AsTCbHxVUUZfje10M9b5NfdwvJFf2/7G LSRFF01DAvNxHBqnmx/aE8dh3k7ioHvfHue1vjUPq7uOC+af56rfBgX64PytEZfOwisduxQSNBfw Wq5+jb822V2Ih2lb/B8/hIN0JouCgH6QZhVGVznBsqrjnWM451UZNpEF4alFgwmpUOXPcPeresbP jCmpe8vSsjIbPnUTgpxdKljNky1+Itm3Zi67f33Q0vSIt5Es9EoJF76glRJeGkLNzPOkhY6U4Nwm pcGPuCEf4Z43ofcLVphtCwl8mpTwF6N+yd2xdXa2jBhIht5kszMU8yuDcd3QbXHil9nVohJvu6B5 vcQRHhpp7PEHhQA50A063668oneXkEyLK/B42pisdQETtnjPfkqpYEwdV9Z5txu5w7fwEZLpA24c 79sI7V+z/cSbtFWEMLqsZqM85AyyKVDKBkiY9v1Rlez6jmoRl5GlOLcFP/Fxqx7uULDQRB3NLRCT LjVJtmyGFtPa4hasZDDUfxy9y9wEUlkimozUEuvb0z+W5I6rkmY74BT7vDdrlhsHmFDqewO9xyOq CYO8P1aHgQR+g6WUCsiTyTZNu0r2Auu4rbGJBPCJ3z7qumxqedKgZZAS9m5ZAKg1qGreZOdFdybi vRCVnvFN4PkHAZwWSL7xbydwCribhNJcin9WPNpdv33FTAj+lpTxru5JxRW4g3h9aDQ/DezVC+Yz bNn3CyG7X+t1TW39Ja77LGPpOwDF+w71RQJs7A+rCbLPqhFeVi+J0Q9NPwSYMQmL+b0U/OC2PAT6 Q7MiCo7rm32+6o357LVs51cVC2TOjdXlLnr1KygtVd32N1s3THnwUUyY+1qbKMBij66D1rOEWOEQ NOh2XN6qAseowdwxD2E3+DaJj3JiOSC6vtxuYy/MBLIoEYiPz+UCKHW+Tw+We3mtTllf3/hoLIlS Lz9Y9klmJpbg 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 On Fri, 2021-07-09 at 09:42 -0700, Steve Midgley wrote: > On Fri, Jul 9, 2021 at 9:03 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; > > > > What do you mean by the "sort fails silently?" Do you mean the query runs and returns data, but the data are not sorted by your name field? > > I modified your sample slightly (to make it work with Pg 9.x in SQLFiddle): http://sqlfiddle.com/#!17/feaff/1 > > I also changed l.id 3 to link to descid 2 (otherwise there's nothing to sort - the records all have the same name). > > It works fine for me.. What am I missing? > > Steve Ha-ha.