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
Solved

Selecting Records Between Time Ranges On Dates

Posted on 2014-12-06
8
128 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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