Re: [SQL] searching time arrays

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Nathan Gelbard <gelbardn(at)intus(dot)net>
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] searching time arrays
Date: 1999-08-19 19:17:45
Message-ID: 8235.935090265@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Nathan Gelbard <gelbardn(at)intus(dot)net> writes:
> given the table below, how would i determine which name is
> avail on a givin day at a givin time? the time arrays
> contain an a list of hours a support person is available;
> '{09:00:00,10:00:00,11:00:00,12:00:00}' etc.
> any thoughts?

> CREATE TABLE support_sched
> (
> name varchar(30),
> monday time[],
> tuesday time[],
> wednesday time[],
> thursday time[],
> friday time[],
> saturday time[],
> sunday time[]
> );

I wouldn't design the table that way --- you've made it really hard
to answer that sort of query, rather than exploiting SQL's strengths.
Instead, consider a table that has one record per availability-window,
say

name text,
dayofweek int2, -- 1-7 = Sun-Sat, or some such encoding
starttime time,
stoptime time

and you just make as many of these as there are contiguous time-
windows in each support person's schedule. There might be twenty
or so records per support person, but so what. Now your query
looks like

SELECT name FROM support_sched
WHERE dayofweek = appropriatevalue AND
now >= starttime AND now <= stoptime;

(you could also reduce the last two clauses to a BETWEEN if you
happen to like syntactic sugar).

If you're concerned about storing twenty or so copies of a person's
name, make that column be an integer ID code instead that you can look
up in a separate table of support people. I dunno if I'd bother with
that if the data is *only* a person's name, but as soon as you start
adding phone number, home address, mother's maiden name, yadda yadda
you will want the separate table...

regards, tom lane

Browse pgsql-sql by date

  From Date Subject
Next Message Mike Field 1999-08-19 21:11:15 multiple keyword search
Previous Message Nathan Gelbard 1999-08-19 18:37:37 searching time arrays