pg.ddx.io pgsql-sql@postgresql.org mailing list archivehelp / color / mirror / Atom feed
From: David Blankley <david@blankley.com>
To: pgsql-sql@postgresql.org
Subject: Re: Dynamic Offset Determination
Date: Wed, 13 Jul 2005 16:06:35 +0000 (GMT)
Message-ID: <20050713160327.H33783-100000@hallsofworlds.com> (raw)
I came up with a clever solution to this myself...
I know the determined value for my CriteriaCol, so I can simply get the
count of all the rows with criteria <= my value...
SELECT count(*) FROM foo WHERE CriteriaCol<=constraint ORDER BY OrderCol;
Thanks,
Dave
-----------------------------------
Problem Statement:
I want to return n rows from a table.
These n rows are relative to an offset.
The part I can't figure out:
The offset needs to be determined from values in the rows.
Example:
Given a table foo:
CriteriaCol integer
OrderedCol integer
I can figure out the number of rows
SELECT COUNT(CriteriaCol) FROM foo;
How do I determine the offset?
SELECT {rownum?} FROM foo WHERE CriteriaCol = SomeValidValue;
With the rownum I can then determine the rows I want.
SELECT * FROM foo ORDER BY OrderedCol LIMIT 21 OFFSET (rownum-10);
Don't worry about boundary conditions, functions, etc, I can sort that out
once I know how to get the offset.
--------------------------
Searching the forums, the one potential hack I've found is:
Quote from: http://archives.postgresql.org/pgsql-sql/2003-04/msg00287.php
create temp sequence foo;
select nextval('foo'), * from
(select ... whatever ... order by something) ss;
drop sequence foo;
Presumably I would get my ordinal value from the sequence before dropping
it.
This seems like it would be a potentially slow process. Also, this
solution dates to 2003, so I thought it worth asking in case a better
solution has come along.
Thanks for suggestions/solutions
Dave
view thread (2+ messages)
Message-ID: <20050713160327.H33783-100000@hallsofworlds.com> Permalink: ../20050713160327.H33783-100000@hallsofworlds.com/ Also on: postgresql.org/message-id/20050713160327.H33783-100000@hallsofworlds.com ·
reply
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Reply to all the recipients using the --to and --cc options: reply via email To: pgsql-sql@postgresql.org Cc: david@blankley.com Subject: Re: Dynamic Offset Determination In-Reply-To: <20050713160327.H33783-100000@hallsofworlds.com> * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox