Solved

Selecting Records Between Time Ranges On Dates

Posted on 2014-12-06
8
131 Views
Last Modified: 2014-12-22
I ma trying to get a set of instructors where instructors do not have reservation on the date and time
for example on the date 1/1/2014 from 6:00AM to 8:00AM

what am I doing wrong
SELECT DISTINCT U.userKey
		, U.firstName
	    , U.lastName
	    , U.email
	FROM users U WITH (NOLOCK)
		INNER JOIN user_securityRole USR WITH (NOLOCK) ON U.userKey = USR.userKey
		INNER JOIN securityRole_lkup_permission SRLP WITH (NOLOCK) ON USR.securityRoleKey = SRLP.securityRoleKey
		INNER JOIN lkup_permission LP WITH (NOLOCK) ON SRLP.permissionKey = LP.permissionKey
	WHERE U.enable = 'Y'
		AND LP.permissionCd = 'INS'
		AND  EXISTS (SELECT  1 from instructors I WITH (NOLOCK) where deleted = 0 and I.instructorKey=u.userKey)
		and  not EXISTS (
			SELECT 1
			FROM Session S WITH (NOLOCK)
			INNER JOIN SessionUnit SU WITH (NOLOCK) ON S.SessionKey = SU.SessionKey
			inner join sessionMap SM WITH (NOLOCK) on SM.sessionKey= S.sessionKey
			WHERE 
			 su.instructorKey = U.userKey
			AND (
                ((<cfqueryparam value="#submittedStartDateTime#" cfsqltype="cf_sql_timestamp"> between DateAdd(minute, -14, SU.sessionStart) 
					and DateAdd(minute, 14, SU.sessionEnd) OR <cfqueryparam value="#submittedEndDateTime#" cfsqltype="cf_sql_timestamp"> between DateAdd(minute, -14, SU.sessionStart) and DateAdd(minute, 14, SU.sessionEnd)) 
				)
                OR ((<cfqueryparam value="#submittedStartDateTime#" cfsqltype="cf_sql_timestamp"> between SU.sessionStart and SU.sessionEnd 
						OR <cfqueryparam value="#submittedEndDateTime#" cfsqltype="cf_sql_timestamp"> between SU.sessionStart and SU.sessionEnd) 
				)
          )
			AND (
        		SELECT COUNT(1)
				FROM sessionMap SMX WITH (NOLOCK)
				WHERE S.sessionKey = SMX.sessionKey
			) > 0
			)
			
		OR (U.userKey = <cfqueryparam cfsqltype="cf_sql_integer" value="#qbtwSession.instructorKey#">)	
		
	ORDER BY U.lastname
		, U.firstname

Open in new window

0
Comment
Question by:erikTsomik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 40484987
Edit - Fix code typo

> do not have reservation on the date and time... 1/1/2014 from 6:00AM to 8:00AM

First, looks like your sql is adding a few minutes before/after the reservation time. So you're actually checking for reservations between:  1/1/2014 5:46 AM and 8:14AM. If so, don't use dateAdd to modify the db column values. That prevents the db from using indexes. Instead just modify your variables:

     <cfset adjustedStartTime = dateAdd("n", -14, submittedStartDateTime)>
     <cfset adjustedEndTime = dateAdd("n", -14, submittedEndDateTime)>

Open in new window


Assuming the rest of your query is correct, the key part is the SessionStart/End query. To find reservations between 1/1/2014 6:00AM and 8:00AM use this comparison:

...
NOT EXISTS
 (
      SELECT 1
      FROM   Session S WITH (NOLOCK)
               INNER JOIN SessionUnit SU WITH (NOLOCK) ON S.SessionKey = SU.SessionKey
               INNER JOIN sessionMap SM WITH (NOLOCK) on SM.sessionKey= S.sessionKey
      WHERE su.instructorKey = U.userKey
      AND   su.SessionStart < <cfqueryparam value="#adjustedEndTime#" cfsqltype="cf_sql_timestamp">
      AND   su.SessionEnd > <cfqueryparam value="#adjustedStartTime#" cfsqltype="cf_sql_timestamp">

 )

Open in new window

0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40485045
I was wondering if the logic is correct I want only include people who do not have any reservation for a specific time and also the people who has not been reserved at all
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40485082
Well the basic construct:

SELECT  ...
FROM    Users u
WHERE NOT EXISTS
        (
               ... existing reservations for u.UserID and date range ...
        )

is correct.  But the original date logic looked a bit off. The new logic should fix that.

You can test it by running the subquery separately. Just set variables for the userID and dates you wan to test.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:erikTsomik
ID: 40485121
How would I also include instructors who has not reservations, because I just want exclude the once that have reservations for that date/time span, but also need the once who has no reservation maybe need to change  something
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40485165
Gah... sorry, that came out wrong.  I meant to say:

You're already doing that. Because you're using a subquery and NOT EXISTS, you're only excluding ones with conflicting reservations. Assuming no errors in the rest of the query, the outer query will still return instructors with no reservations at all or ones with no conflicting reservations during that time period.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40485168
when using the time range do I need to end time to be 7:59 or 8:01
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40485182
Your subquery is checking for conflictingreservations. So use whatever time would constitute a conflict. For example, if you want exclude the user if there's a reservation for 7:59 - then use that time.

Edit: I'm heading out. I'll check back tomorrow.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40485184
OK.thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question