Re: Counting Row

From: PFC <lists(at)boutiquenumerique(dot)com>
To: ricky(at)connexiasolutions(dot)com, pgsql-sql(at)postgresql(dot)org
Subject: Re: Counting Row
Date: 2005-07-24 16:45:12
Message-ID: op.suffhmy3th1vuj@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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=#)

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-07-24 18:41:44 Re: Different encodings in different DBs in same cluster
Previous Message Jim Buttafuoco 2005-07-23 12:57:38 Re: Multi-column returns from pgsql