I have a table containing timeslots for booking appointments and i try to figure out a way with a sql statement/view to find adjacent free timeslots for appointments of different duration.
The create table looks like this:
CREATE TABLE timeslot ( timeslot_id bigserial NOT NULL, duration bigint, successor bigint, predecessor bigint, start_year character varying NOT NULL, start_month character varying NOT NULL, start_day character varying NOT NULL, start_hour character varying NOT NULL, start_minute character varying NOT NULL, end_year character varying NOT NULL, end_month character varying NOT NULL, end_day character varying NOT NULL, end_hour character varying NOT NULL, end_minute character varying NOT NULL, employee_id integer NOT NULL, available_status_id integer, appoint_calendar_id integer CONSTRAINT timeslot_id PRIMARY KEY (timeslot_id), CONSTRAINT appoint_calendar_id FOREIGN KEY (appoint_calendar_id) REFERENCES appoint_calendar (appoint_calendar_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT available_status_id FOREIGN KEY (available_status_id) REFERENCES available_status (available_status_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT employee_id FOREIGN KEY (employee_id) REFERENCES employee (employee_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) Here is example insert data where a available_status_id of 1 means a free timeslot and a available_status_id of 2 is a free timeslot:
INSERT INTO timeslot( timeslot_id, duration, successor, predecessor, start_year, start_month, start_day, start_hour, start_minute, end_year, end_month, end_day, end_hour, end_minute, employee_id, available_status_id, appoint_calendar_id) VALUES (11870, 30, null, 11869, "2013", "09", "02", "18", "00", "2013", "09", "02", "18", "30", 4, 1, null); INSERT INTO timeslot( timeslot_id, duration, successor, predecessor, start_year, start_month, start_day, start_hour, start_minute, end_year, end_month, end_day, end_hour, end_minute, employee_id, available_status_id, appoint_calendar_id) VALUES (11904, 30, 12000, 11999, "2013", "09", "09", "10", "30", "2013", "09", "09", "11", "00", 5, 2, 761); I am looking for a query in postgres to find all free timeslots for appointments of different durations like 15, 30 or 60 minutes. At the moment I just get all free timeslots from the database and iterate over them in Java and add together the minutes of duration until I have found enough adjacent timeslots and return then the first timeslot for each subgroup to be displayed in a calendar. But there must be a better and quicker way in postgres? Thanks in advance
Edit
Input is the needed duration in minutes (e.g 60), the employee_id (e.g. 5) and a date (e.g. 09.09.2013). Required output are all subsets that are adjacent (in time), free and have enough duration. For the above example this could be:
timeslot_id 11904 duration 30 successor 12000 predecessor 11999 start_year 2013 start_month 09 start_day 09 start_hour 10 start_minute 30 end_year 2013 end_month 09 end_day 09 end_hour 11 end_minute 00 employee_id 5 available_status_id 1 appoint_calendar_id null
and
timeslot_id 12000 duration 30 successor 11906 predecessor 11904 start_year 2013 start_month 09 start_day 09 start_hour 11 start_minute 00 end_year 2013 end_month 09 end_day 09 end_hour 11 end_minute 30 employee_id 5 available_status_id 1 appoint_calendar_id null
"2013"refers to a column named2013,'2013'is a string with the value2013. Additionally why are you storing numbers as characters? If you choose to split up a date into it's components it makes much more sense to at least define those components as integers. Storing numbers in character columns is almost always a very bad idea. And I agree with wildplasser: using timestamps would make much more sense.