|
|
|
|
|
|
| Author |
Message |
Benjamin Smith *nix forums beginner
Joined: 12 Mar 2005
Posts: 30
|
Posted: Sat Mar 12, 2005 7:13 am Post subject:
Checking for schedule conflicts
|
|
|
Given the tables defined below, what's the easiest way to check for schedule
conflicts?
So far, the only way I've come up with is to create a huge, multi-dimensional
array in PHP, with a data element for every minute of all time taken up by
all events, and then check for any of these minutes to be set as I go through
all the records. (ugh!)
But, how could I do this in the database?
But I'd like to see something like
"select count(*) FROM events, sched
WHERE sched.date=$date
AND events.id=sched.events_id
...
GROUP BY date, start<finish and finish<start
HAVING count(*) >1 "
And here's where I get stumped. You can't group by start or end because we
need to check if they OVERLAP any other records on the same date.
Ideas?
// Sometimes, recurring events
create table events (
id serial not null primary key,
title varchar
);
// date=YYYYMMDD, start/end: HH:MM (24-hour)
create table sched (
events_id integer not null references events(id),
date integer not null,
start integer not null,
end integer not null
);
insert into events (title)
VALUES ('Tuesday Pickup');
insert into sched(events_id, date, start, end)
VALUES (1, 20050308, 0900, 1300);
insert into sched (events_id, date, start, end)
VALUES (1, 20050315, 0900, 1300);
--
"The best way to predict the future is to invent it."
- XEROX PARC slogan, circa 1978
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend |
|
| Back to top |
|
 |
Ragnar Hafstaš *nix forums beginner
Joined: 01 Mar 2005
Posts: 37
|
Posted: Sat Mar 12, 2005 9:13 am Post subject:
Re: Checking for schedule conflicts
|
|
|
On Sat, 2005-03-12 at 00:13 -0800, Benjamin Smith wrote:
| Quote: | Given the tables defined below, what's the easiest way to check for schedule
conflicts?
So far, the only way I've come up with is to create a huge, multi-dimensional
array in PHP, with a data element for every minute of all time taken up by
all events, and then check for any of these minutes to be set as I go through
all the records. (ugh!)
But, how could I do this in the database?
But I'd like to see something like
"select count(*) FROM events, sched
WHERE sched.date=$date
AND events.id=sched.events_id
...
GROUP BY date, start<finish and finish<start
HAVING count(*) >1 "
And here's where I get stumped. You can't group by start or end because we
need to check if they OVERLAP any other records on the same date.
Ideas?
|
use the OVERLAPS operator ?
http://www.postgresql.org/docs/8.0/interactive/functions-datetime.html
gnari
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match |
|
| Back to top |
|
 |
Andre Maasikas *nix forums beginner
Joined: 14 Mar 2005
Posts: 2
|
Posted: Mon Mar 14, 2005 5:06 pm Post subject:
Re: Checking for schedule conflicts
|
|
|
Ragnar Hafstaš wrote:
| Quote: | On Sat, 2005-03-12 at 00:13 -0800, Benjamin Smith wrote:
Given the tables defined below, what's the easiest way to check for schedule
conflicts?
So far, the only way I've come up with is to create a huge, multi-dimensional
array in PHP, with a data element for every minute of all time taken up by
all events, and then check for any of these minutes to be set as I go through
all the records. (ugh!)
But, how could I do this in the database?
But I'd like to see something like
"select count(*) FROM events, sched
WHERE sched.date=$date
AND events.id=sched.events_id
...
GROUP BY date, start<finish and finish<start
HAVING count(*) >1 "
And here's where I get stumped. You can't group by start or end because we
need to check if they OVERLAP any other records on the same date.
Ideas?
use the OVERLAPS operator ?
http://www.postgresql.org/docs/8.0/interactive/functions-datetime.html
gnari
|
The idea is to join table with itself so you can
compare different records, something like:
select * from sched a, sched b /* join with itself */
where (a.start between b.start and b.end /* filter out overlapping */
or a.end between b.start and b.end)
and a.id != b.id /* event overlaps iself - leave that out */
or insted of 'between' use the OVERLAPS operator Ragnar mentioned when
dealing with date types.
Andre
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster |
|
| Back to top |
|
 |
Greg Sabino Mullane *nix forums beginner
Joined: 07 Mar 2005
Posts: 47
|
Posted: Tue Mar 15, 2005 12:41 am Post subject:
Re: Checking for schedule conflicts
|
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Benjamin Smith wrote:
| Quote: | Given the tables defined below, what's the easiest way to check for schedule
conflicts?
|
Assuming you don't count more than once things with the same id, start, and finish:
SELECT COUNT(DISTINCT (s1.event_id||s1.start||s1.finish))
FROM sched s1, sched s2
WHERE s1.date = s2.date AND s1.start >= s2.start
AND s2.finish <= s2.finish AND NOT s1.ctid = s2.ctid
Add "AND s1.date = $date" as needed.
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200503140639
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----
iD8DBQFCNXgTvJuQZxSWSsgRAlBmAKC7hj8XzHzS7srqfgdSGOZiCfvtDQCfWM22
VXMkQB7IzEdTKjqpcmWVdaM=
=hFiy
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org) |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Fri Jan 09, 2009 6:24 am | All times are GMT
|
|
Mbna | Personal Finance | Credit Counseling | Business Credit Cards | Agencia de viagens
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|