Solved

Selecting Records Between Time Ranges On Dates

Posted on 2014-12-06
8
133 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
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!

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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