X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id EFE38D1B57B for ; Wed, 22 Oct 2003 22:14:31 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 00990-10 for ; Wed, 22 Oct 2003 19:14:03 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 1EF25D1B538 for ; Wed, 22 Oct 2003 19:14:01 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id h9MME0Nu023284 for ; Wed, 22 Oct 2003 22:14:00 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h9MLwBgc020261 for pgsql-sql@postgresql.org; Wed, 22 Oct 2003 21:58:11 GMT From: "Timo" X-Newsgroups: comp.databases.postgresql.sql Subject: A tricky sql-query... Date: Thu, 23 Oct 2003 00:58:08 +0300 Organization: Hub.Org Networking Services Lines: 101 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-Spam-Status: No, hits=1.5 tagged_above=0.0 required=5.0 tests=PRIORITY_NO_NAME, RCVD_IN_RFCI X-Spam-Level: * X-Archive-Number: 200310/315 X-Sequence-Number: 15715 We have a small association and the association has a cabin. Members of the association can rent a term to stay in the cabin but as the cabin has turned out to be very famous we have had to establish an application policy for that. It goes like this: 1. There's a seniority queue for this purpose (once you've got a term you'll be placed in the last position in the queue) 2. Members can apply for one or more of the terms 3. The top one member in this seniority queue gets the term he applies. 4. The second member in the queue gets the term he primarly applies unless it's not being taken by the first member. If this is the case then take his secondary quest. 5. The third member gets the term he's primarly applied unless it's not being taken by the first or the second applicant. If it is then try his secondary application. If that's taken as well then try his 3rd quest (if he has such) 6. and so on.. So, (if you didn't understand anything it's OK, pardon my poor English) if I have a table for the applies: CREATE TABLE apply_demo ( memberid integer, sen integer, priority integer, termid integer ); INSERT INTO apply_demo VALUES (2041, 115, 1, 15); INSERT INTO apply_demo VALUES (2041, 115, 2, 18); INSERT INTO apply_demo VALUES (2041, 115, 3, 19); INSERT INTO apply_demo VALUES (206, 120, 1, 13); INSERT INTO apply_demo VALUES (6571, 184, 1, 16); INSERT INTO apply_demo VALUES (123340, 213, 1, 4); INSERT INTO apply_demo VALUES (123340, 213, 2, 16); INSERT INTO apply_demo VALUES (123340, 213, 3, 9); INSERT INTO apply_demo VALUES (152946, 301, 1, 5); INSERT INTO apply_demo VALUES (152880, 302, 1, 13); INSERT INTO apply_demo VALUES (152880, 302, 2, 14); INSERT INTO apply_demo VALUES (181333, 332, 1, 17); INSERT INTO apply_demo VALUES (242502, 462, 1, 9); INSERT INTO apply_demo VALUES (246024, 473, 1, 18); INSERT INTO apply_demo VALUES (246024, 473, 2, 19); INSERT INTO apply_demo VALUES (246024, 473, 3, 13); INSERT INTO apply_demo VALUES (245954, 475, 1, 11); INSERT INTO apply_demo VALUES (245954, 475, 2, 12); INSERT INTO apply_demo VALUES (245954, 475, 3, 16); INSERT INTO apply_demo VALUES (245954, 475, 4, 8); INSERT INTO apply_demo VALUES (152972, 510, 1, 13); INSERT INTO apply_demo VALUES (152972, 510, 2, 4); INSERT INTO apply_demo VALUES (152972, 510, 3, 16); INSERT INTO apply_demo VALUES (152972, 510, 4, 22); INSERT INTO apply_demo VALUES (152972, 510, 5, 2); INSERT INTO apply_demo VALUES (254085, 537, 1, 8); INSERT INTO apply_demo VALUES (288842, 640, 1, 8); I'd need to get out something like this: termid | gotby --------+-------- 2 | 3 | 4 | 123340 5 | 152946 6 | 7 | 8 | 254085 9 | 242502 10 | 11 | 245954 12 | 13 | 206 14 | 152880 15 | 2041 16 | 6571 17 | 181333 18 | 246024 19 | 20 | 21 | 22 | 152972 (21 rows) I know you Gurus are busy and as you are, don't spend too much time on this because it has already been implemented with PL/PgSQL. But just out of the curiosity - and for the educational purposes :) - I'd like to know whether you can do this with a single sql-query? You can't have any recursion in an pure sql-query, can you? Regards, Timo