pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedTough Problem -- Record Checkouts
2+ messages / 2 participants
[nested] [flat]
* Tough Problem -- Record Checkouts
@ 2006-02-13 17:33 Alfred <99m@myway.com>
0 siblings, 1 reply; 2+ messages in thread
From: Alfred @ 2006-02-13 17:33 UTC (permalink / raw)
To: pgsql-sql
Imagine a library of books. Each book is a record. The library is the
table. A user may check out a book and the shelf space it once occupied
will store the minute that the user checked the book out. Every 5
minutes, a magical librarian walks through the library and when a book
has been checked out longer than 15 minutes, she has the power to zap
it back out of the user's hands and put it back on the shelf for
someone else. How do you efficiently achieve this in a WHERE clause in
SQL?
For instance, here's a table of several minute columns. CO, in this
case, is the checked out minute. N, in this case, is the current
minute. This translates to, "If the CO = x, and N is within this range,
then clear the CO column."
CO | N
------+-----------------------
0 | 15-59
1 | 0, 16-59
2 | 0-1, 17-59
15 | 0-14, 30-59
16 | 0-15, 31-59
30 | 0-29, 45-59
31 | 0-30, 46-59
45 | 0-44
46 | 1-45
59 | 14-58
This becomes some kind of UPDATE statement with a complex WHERE clause.
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Tough Problem -- Record Checkouts
@ 2006-02-18 07:58 Greg Stark <gsstark@mit.edu>
parent: Alfred <99m@myway.com>
0 siblings, 0 replies; 2+ messages in thread
From: Greg Stark @ 2006-02-18 07:58 UTC (permalink / raw)
To: Alfred <99m@myway.com>; +Cc: pgsql-sql
"Alfred" <99m@myway.com> writes:
> CO | N
> ------+-----------------------
> 0 | 15-59
> 1 | 0, 16-59
> 2 | 0-1, 17-59
> 15 | 0-14, 30-59
> 16 | 0-15, 31-59
> 30 | 0-29, 45-59
> 31 | 0-30, 46-59
> 45 | 0-44
> 46 | 1-45
> 59 | 14-58
>
> This becomes some kind of UPDATE statement with a complex WHERE clause.
That's usually a sign you're storing the data in the wrong form. Why are you
storing just the minutes and not the absolute timestamp of the checkout?
In any case the where clause isn't all that complex. I think you've confused
yourself by drawing the inverse table of what you need. You need the range of
CO to expire for a given N, not the range of N in which you should expire a
given CO.
WHERE co BETWEEN 0 AND n-15 OR co BETWEEN n+1 AND n+45
--
greg
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2006-02-18 07:58 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-02-13 17:33 Tough Problem -- Record Checkouts Alfred <99m@myway.com>
2006-02-18 07:58 ` Greg Stark <gsstark@mit.edu>
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