• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

Selecting Records Between Time Ranges On Dates

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
erikTsomik
Asked:
erikTsomik
  • 4
  • 4
1 Solution
 
_agx_Commented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
_agx_Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
erikTsomikSystem Architect, CF programmer Author Commented:
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
 
_agx_Commented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
when using the time range do I need to end time to be 7:59 or 8:01
0
 
_agx_Commented:
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
 
erikTsomikSystem Architect, CF programmer Author Commented:
OK.thanks
0

Featured Post

Technology Partners: 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!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now