pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedDynamic Offset Determination
2+ messages / 1 participants
[nested] [flat]
* Dynamic Offset Determination
@ 2005-07-13 15:28 David Blankley <david@blankley.com>
0 siblings, 0 replies; 2+ messages in thread
From: David Blankley @ 2005-07-13 15:28 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Dynamic Offset Determination
@ 2005-07-13 16:06 David Blankley <david@blankley.com>
0 siblings, 0 replies; 2+ messages in thread
From: David Blankley @ 2005-07-13 16:06 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2005-07-13 16:06 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-07-13 15:28 Dynamic Offset Determination David Blankley <david@blankley.com>
2005-07-13 16:06 Re: Dynamic Offset Determination David Blankley <david@blankley.com>
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