X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id CAA19D1B541 for ; Mon, 29 Sep 2003 14:32:58 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 98604-02 for ; Mon, 29 Sep 2003 11:32:33 -0300 (ADT) Received: from news.hub.org (unknown [64.117.224.194]) by svr1.postgresql.org (Postfix) with ESMTP id E3544D1B538 for ; Mon, 29 Sep 2003 11:32:29 -0300 (ADT) Received: from news.hub.org (host-64-117-224-194.altec1.com [64.117.224.194] (may be forged)) by news.hub.org (8.12.9/8.12.9) with ESMTP id h8TEWThK068476 for ; Mon, 29 Sep 2003 14:32:29 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h8TERm69054134 for pgsql-sql@postgresql.org; Mon, 29 Sep 2003 14:27:48 GMT From: "Timo" X-Newsgroups: comp.databases.postgresql.sql Subject: Need to overcome UNION / ORDER BY restriction Date: Mon, 29 Sep 2003 17:27:47 +0300 Organization: Hub.Org Networking Services Lines: 68 Message-ID: X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/390 X-Sequence-Number: 15368 I have a table: id | priority | seniority -----+----------+----------- 902 | 2 | 271 902 | 1 | 271 902 | 3 | 271 924 | 2 | 581 924 | 1 | 581 924 | 3 | 581 935 | 1 | 276 935 | 2 | 276 972 | 2 | 275 (9 rows) I'd need to get a result set where rows are sorted according to these rules: 1. first all rows with priority = 1 sorted according to seniority 2. then the rest of the rows sorted by seniority, priority Something like this: SELECT * from foo where priority = 1 order by seniority union select * from foo where priority > 1 order by seniority, priority but this gives parse error because of the restrictions with ORDER BY and UNION (I suppose..) select * from foo order by case when priority = 1 then priority else seniority end; goes fine, but it's not quite enough and when I try select * from foo order by case when priority = 1 then priority else seniority, priority end; it's parse error at or near ",". Any suggestions? Thanks in advance, Timo --------------- CREATE TABLE foo ( id integer, priority integer, seniority integer ); INSERT INTO foo VALUES (902, 2, 271); INSERT INTO foo VALUES (902, 1, 271); INSERT INTO foo VALUES (902, 3, 271); INSERT INTO foo VALUES (924, 2, 581); INSERT INTO foo VALUES (924, 1, 581); INSERT INTO foo VALUES (924, 3, 581); INSERT INTO foo VALUES (935, 1, 276); INSERT INTO foo VALUES (935, 2, 276); INSERT INTO foo VALUES (972, 2, 275);