Solved

Selecting Records Between Time Ranges On Dates

Posted on 2014-12-06
8
125 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now