sql - PostgreSQL SELECT Must Match Across Multiple Rows -


I have a table in which there is a pair of timestamp which represents a timepan, these lines are scotched by user id , And each user may have one or more rows.

This data is generated from an abstract "availability" form which indicates when the user becomes available during the week. I need to input a series of time limits as a query and return all the User ID, for which all the rows are in the table match.

Considering this table:

  

and this data:

  User # 1 is available Mon- Between Tuesdays 08:00 and 17:00 - - + --------- + --------------------- + ----- ---------- ------ + | ID | User_id | Start_at | Ends. + ---- + --------- + --------------------- + ------------ --------- + | 1 | 1 | 2013-03-18 08:00:00 | 2013-03-18 17:00:00 | + ---- + --------- + --------------------- + ------------ --------- + | 2 | 1 | 2013-03-19 08:00:00 | 2013-03-19 17:00:00 | + ---- + --------- + --------------------- + ------------ --------- + User # 2 is available Sun-all-day + ---- + --------- + -------------- ------- + --------------------- + | 3 | 2 | 2013-03-17 00:00:00 | 2013-03-23 ​​23:59:59 | + ---- + --------- + --------------------- + ------------ --------- + User # 3 is available between Wed 06:00 and 18:00 + ---- + --------- + --------- - ----------- + --------------------- + | 4 | 3 | 2013-03-20 06:00:00 | 2013-03-20 18:00:00 | + ---- + --------- + --------------------- + ------------ --------- +   

I can easily select users who are available for any given timestamp < / P>

 "select" from "location" where ('2013-03-19 08:35:00' start_and end_at and '2013-03-19 18:25:00' between start_and end_at) or (Beginning and end_at between '2013-03-20 12:00:00' and '2013-03-20 18:00:00' begins and ends); + ---- + --------- + --------------------- + ------------ --------- + | ID | User_id | Start_at | Ends. + ---- + --------- + --------------------- + ------------ --------- + | 3 | 2 | 2013-03-17 00:00:00 | 2013-03-23 ​​23:59:59 | + ---- + --------- + --------------------- + ------------ --------- + | 4 | 3 | 2013-03-20 06:00:00 | 2013-03-20 18:00:00 | + ---- + --------- + --------------------- + ------------ --------- +   

But whatever needs I need, to examine several timepoints and simply return user_id s to that match Has to be able to all .

Question: 2013-03-17 10:00:00 - 2013-03-17 16:00: 00 , 2013- 03-23 ​​10:00:00 - 2013-03-23 ​​16:00:00 should come back:

  + - --- + --------- + --------------------- + -------- ------ ------- + | ID | User_id | Start_at | Ends. + ---- + --------- + --------------------- + ------------ --------- + | 3 | 2 | 2013-03-17 00:00:00 | 2013-03-23 ​​23:59:59 | + ---- + --------- + --------------------- + ------------ --------- +   

Question: 2013-03-18 09:00:00 - 2013-03-18 16 : 00: 00 , 2013-03-19 08:00:00 - 2013-03-19 15:45:00 should come back:

  + ---- + --------- + --------------------- + ---- - ---------------- + | ID | User_id | Start_at | Ends. + ---- + --------- + --------------------- + ------------ --------- + | 1 | 1 | 2013-03-18 08:00:00 | 2013-03-18 17:00:00 | + ---- + --------- + --------------------- + ------------ --------- + | 2 | 1 | 2013-03-19 08:00:00 | 2013-03-19 17:00:00 | + ---- + --------- + --------------------- + ------------ --------- + | 3 | 2 | 2013-03-17 00:00:00 | 2013-03-23 ​​23:59:59 | + ---- + --------- + --------------------- + ------------ --------- +   

Question: 2013-03-18 07:00:00 - 2013-03-18 18 : 00: 00 should not be returned.

This boolean artist will have 0 or 1 As the integer completes.

  Select one. * Available from an internal (available user_id, select sum ((between 'start_at and end_at' between start_at and end_at and '2013-03-18 16:00:00' 2013-03-18 09: 00): : Integer + ('2013-03-19 08:00:00' start_at and end_at and '2013-03-19 15:45:00' between start_at and end_at) :: integer) by user_id by availability group) .user_id = s.user_id where the period is & gt; = 2   

where condition change to the number of match duration.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -