X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9FE2B529B1 for ; Sun, 24 Jul 2005 13:45:16 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 13596-10 for ; Sun, 24 Jul 2005 16:45:15 +0000 (GMT) Received: from boutiquenumerique.com (boutiquenumerique.com [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id E395D52994 for ; Sun, 24 Jul 2005 13:45:13 -0300 (ADT) Received: (qmail 1537 invoked from network); 24 Jul 2005 18:45:14 +0200 Received: from unknown (HELO localhost) (boutiquenumerique-lists@192.168.0.4) by boutiquenumerique.com with SMTP; 24 Jul 2005 18:45:14 +0200 Date: Sun, 24 Jul 2005 18:45:12 +0200 To: ricky@connexiasolutions.com, pgsql-sql@postgresql.org Subject: Re: Counting Row References: <20050721103304.2209252B23@svr1.postgresql.org> From: PFC Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID: In-Reply-To: <20050721103304.2209252B23@svr1.postgresql.org> User-Agent: Opera M2/8.0 (Linux, build 1095) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/215 X-Sequence-Number: 22193 I'd suggest : - first getting the 'order fields' value for the ticket you want : SELECT field_order FROM mytable WHERE condition AND identifier=the_one_you're_looking_for ORDER BY field_order DESC LIMIT 1 - then counting all the tickets up to this order : SELECT count(*) FROM mytable WHERE condition AND field_order <= (SELECT field_order FROM mytable WHERE condition AND identifier=the_one_you're_looking_for ORDER BY field_order DESC LIMIT 1) You could also use a plpgsql function to reimplement your PHP thing, which should be faster than doing it in PHP, but probably slower than the example above. # But there is a simpler solution. How do you model the position of a ticket in the queue ? What is the 'order field' you mention ? If your table has a SERIAL PRIMARY KEY, it's monotonous, so you can use this without further worries. table tickets_queues (id SERIAL PRIMARY KEY, queue_id, ticket_id, UNIQUE( queue_id, ticket_id) ) to get the position of a ticket (by its ticket_id) in a queue : SELECT count(*) FROM tickets_queue WHERE queue_id=# AND id <= (SELECT id FROM tickets_queue WHERE ticket_id=# AND queue_id=#)