Link to home
Start Free TrialLog in
Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on

contiguous time slot

I am trying to filter out the result . from the post made before .https://www.experts-exchange.com/questions/28676589/contiguous-time-slot.html?anchorAnswerId=40791909#a40791909

THe definitions are here
1. Contiguous block - session for the same instructor (car does not matter) within 30 minutes
2.Adjoining sessions - session for a different instructor (in the same car withing 30 minutes of another instructor session)

DECLARE @CancelTime   datetime
SET @CancelTime = '2015-07-01 10:30:00'

 
            DECLARE @DataFromYourTables TABLE (
                sessionKey      int         NOT NULL,
                locationKey     smallint,
                sessionStart    datetime    NOT NULL,
                sessionEnd      datetime    NOT NULL,
                instructorKey   int         NOT NULL,
                Flag            int,
                carKey          smallint    NOT NULL,
                cnt             int,
				bookingLock     int,
				hubLock         int  
            )
            
            INSERT INTO @DataFromYourTables
            
            SELECT  s.sessionKey,
                    s.locationKey,
                    su.sessionStart,
                    su.sessionEnd,
                    su.instructorKey,
                    su.btwSeatsOverride AS Flag,
                    s.carKey,
                    (SELECT count(sm.sessionKey) from sessionMap SM
                        where SM.sessionKey = s.sessionKey
                    )cnt,
					isNUll(S.bookingLock,0) as bookingLock,
					isNUll(S.hubLock,0) as hubLock
            FROM    session s
            JOIN    sessionunit su ON su.sessionKey = s.sessionKey
            JOIN    product     p  ON p.productKey = s.productKey AND p.productTypeKey IN (2,4)
            WHERE   CONVERT(date, su.sessionStart) = CONVERT(date, @CancelTime)
            AND    (su.instructorKey = 36754 OR s.carKey = 139)
       
            
            DECLARE @Results TABLE (
                sessionKey      int,
                locationKey     smallint,
                instructorKey   int,
                sessionStart    datetime,
                sessionEnd      datetime,
                carKey          smallint,
                Flag            int,
                FirstKey        int,
                cnt             int,
				bookingLOck     int,
				hubLock         int 
           )
           
           ;WITH myCTE AS
                (
                SELECT  sessionKey,
                        locationKey,
                        instructorKey,
                        sessionStart,
                        sessionEnd,
                        carKey,
                        Flag,
                        sessionKey AS FirstKey,
                        cnt,
						bookingLock,
						hubLock
                FROM    @DataFromYourTables
                WHERE   sessionStart = @CancelTime
				
                
                UNION ALL
                SELECT  a.sessionKey,
                        a.locationKey,
                        a.instructorKey,
                        a.sessionStart,
                        a.sessionEnd,
                        a.carKey,
                        a.Flag,
                        FirstKey,
                        a.cnt,
						a.bookingLock,
						a.hubLock
                FROM    @DataFromYourTables a
                JOIN    myCTE b
                ON      b.sessionEnd BETWEEN DATEADD(n, -29, a.SessionStart) AND DATEADD(n, 29, a.SessionStart) and b.instructorKey = a.instructorKey 
                )
                INSERT INTO @Results
                SELECT * FROM myCTE

      
                ;WITH myCTE AS
                (
                SELECT  sessionKey,
                        locationKey,
                        instructorKey,
                        sessionStart,
                        sessionEnd,
                        carKey,
                        Flag,
                        sessionKey AS FirstKey,
                        cnt,
						bookingLock,
						hubLock
                FROM     @DataFromYourTables
                WHERE   sessionStart = @CancelTime
                UNION ALL
                SELECT  a.sessionKey,
                        a.locationKey,
                        a.instructorKey,
                        a.sessionStart,
                        a.sessionEnd,
                        a.carKey,
                        a.Flag,
                        FirstKey,
                        a.cnt,
						a.bookingLock,
						a.hubLock
                FROM    @DataFromYourTables a
                JOIN    myCTE b
                ON      b.sessionStart BETWEEN DATEADD(n, -29, a.SessionEnd) AND DATEADD(n, 29, a.SessionEnd) 
				and b.instructorKey <> a.instructorKey 
				and b.carKey = a.carKey
				
                )

                INSERT INTO @Results
                SELECT * FROM myCTE
               WHERE  sessionKey <> FirstKey
           
				
		        
                SELECT  *,
                        CASE
                            WHEN sessionKey <> FirstKey THEN 0
                            ELSE 1
                        END Comment
                FROM    @Results
                ORDER BY sessionStart

Open in new window

Avatar of dsacker
dsacker
Flag of United States of America image

Please do the following:

1.

Please show the results of the query above.

2.

Please show what the results actually should be.If you need to give me some more data, that will help too, else I'll use the mockup data from the previous question.
Avatar of erikTsomik

ASKER

the data that is shown is

sessionKey      locationKey      instructorKey      sessionStart      sessionEnd      carKey      Flag      FirstKey      cnt      Comment
164144      13      36754      2015-07-01 10:30:00.000      2015-07-01 12:30:00.000      139      NULL      164144      1      
164103      34      12383      2015-07-01 08:15:00.000      2015-07-01 10:15:00.000      139      NULL      164144      0      
164202      34      8      2015-07-01 06:00:00.000      2015-07-01 08:00:00.000      139      NULL      164144      0      0      

the data that should be is: The third record should removed because is an adjoin session to the cancelled session
sessionKey      locationKey      instructorKey      sessionStart      sessionEnd      carKey      Flag      FirstKey      cnt      Comment
164144      13      36754      2015-07-01 10:30:00.000      2015-07-01 12:30:00.000      139      NULL      164144      1      
164103      34      12383      2015-07-01 08:15:00.000      2015-07-01 10:15:00.000      139      NULL      164144      0      
1
Please do the following:

In your query, right after you complete your first INSERT statement (after line 36), add this line:

SELECT * FROM @DataFromYourTables
Run your query only up to that line (you can highlight from the top to that line, and it will only run that).

I need to see what's in your @DataFromYourTables.

Don't forget to take out (or comment out) that SELECT statement afterwards, so that it doesn't remain in there permanently.
sessionKey      locationKey      sessionStart      sessionEnd      instructorKey      Flag      carKey      cnt      bookingLock      hubLock      newFlag
164192      13      2015-07-01 06:00:00.000      2015-07-01 08:00:00.000      36754      NULL      133      0      1      0      0
164202      34      2015-07-01 06:00:00.000      2015-07-01 08:00:00.000      8      NULL      139      0      0      1      0
164103      34      2015-07-01 08:15:00.000      2015-07-01 10:15:00.000      12383      NULL      139      0      0      1      0
164144      13      2015-07-01 10:30:00.000      2015-07-01 12:30:00.000      36754      NULL      139      1      1      0      0
164104      NULL      2015-07-01 13:00:00.000      2015-07-01 15:00:00.000      12383      NULL      139      0      0      0      0
164109      NULL      2015-07-01 15:15:00.000      2015-07-01 17:15:00.000      2644      NULL      139      0      0      0      0
164111      NULL      2015-07-01 17:30:00.000      2015-07-01 19:30:00.000      2644      NULL      139      0      0      0      0
164194      NULL      2015-07-01 19:45:00.000      2015-07-01 21:45:00.000      13783      NULL      139      0      0      0      0
What's that "newFlag" at the end? Is that the only new field that I don't see in your @DataFromYourTables layout above (lines 5-16)?  I assume it's an "int" field, but  I don't see it in the SELECT query, so curious what it is.
I was just trying to come up with some ideas you can disregard this
What is the definition or the rule for an adjoin? Is there any data that you sent me that will tell me why 164202 is an adjoin?

I know it's a lot of questions, but we must connect the dots to the adjoin that you don't want in the list.
The data will not tell you that is adjoin.

THe definitions are here
1. Contiguous block - session for the same instructor (car does not matter) within 30 minutes
2.Adjoining sessions - session for a different instructor (in the same car withing 30 minutes of another instructor session from the cancelled session)
Would the above definition #2 (an adjoin) session be a different instructor who does NOT have a cancelled session?

If the session is cancelled, is there a value in su.btwSeatsOverride?
All adjoining sessions will have a different instructor from the session that  being cancelled. Disregard btwSeatsOverride its being being used for different purpose.
What the cancelled means it just being remove from the client account, that said that this session becomes available for scheduling
I'm more confused. Let's see if we can communicate from another angle.

I need to determine from looking at these three rows why 164202 should not be there. The data has to be able to tell me that.

User generated imageWhat makes this confusing is that 164103 has a different instructor, but you want to keep him. And yet, 164202 also has a different instructor, but you want to keep him.

Verify if what you mean by adjoining sessions with different instructor is this:

If the immediately next session is a different instructor, then it must be the same instructor in any further contiguous records.
That would mean, that since instructor 12383 is different from 36754, that session 164202 would have been okay if the instructor there was 12383 (and not 8).
Is that what that means?
164202 session is within 30 minutes from start and end that why I want to keep him
For each canceled session there would be 2 adjoining sessions maximum. 1 from the start  and 1 from the end time
You said you did NOT want to keep164202 in post #40792159.

In that post you first listed the three records that the query is returning.

Then you listed two records, which did NOT have 164202. You said it should not be there.

But just above (two posts) you now say we should keep 164202.

You're really confusing me now.
Sorry you are correct I don't want that session
Please re-read my post #40792275  carefully, and please verify my assumptions in it.
The assumption is incorrect. The cancelled session is 164144. Then we need to check for contiguous session (sessions for the same instructor within 30 minutes). In our case there is no contiguous sessions. Then check  for adjoing sessions (sessions for a different instructor with the same car as a cancelled session within 30 minutes). In our case is the session 164103. (for adjoining sessions find a contiguous sessions ). We can only have 2 adjoining session maximum. Fox example, in  our case it will be session164103 and if we would have a session from 12:45 till 2:45,but we don't having so it is only 1 adjoing session in our case.

Session 164202 is not adjoining session because is more than 30 minutes from start to end from the cancelled session
I appreciate the explanation above. That helps. This is my rewording of an adjoining session:

So an adjoining session is another session in the same time frame, as long as that session uses the same car and is a cancelled session? For instance, sessions 164192 and 164202 have the same time frame, which means they could be adjoining if they only had the same cars, but because they are different cars, that disqualifies them as adjoining?
If this is now the correct assumption, then:

It seems we really need to know whether that adjoining session is cancelled or not. Previously you said the field btwSeatsOverride tells that, but then later you said it doesn't.
We probably will need to verify that adjoining records are truly eligible after we have loaded the @DataFromYourTables.
Which field in the session, sessionunit or product tables tells if a session is cancelled? I'm not 100% sure it is essential to know this (perhaps you want to decide that online), but it sure seems so, as we need to determine whether an adjoining (or contiguous) session is eligible.
We do not need to know if the adjoining session have been cancelled. The important key IT MUST BE 30 MINUTES from start and END. Disregard the thing about cancelled sessions. The only cancelled sessions is the session that we currently cancelling
Okay, I won't worry about cancelled sessions. One more clarification, and I think we can march on. Wouldn't the important key be:

It must be 30 minutes from start and END (i.e., a later session), OR
It must be 30 minutes from end and START (i.e., a prior session)
??
A little side note: I've been doing SQL, .NET development and projects for over 35 years. These are the kinds of questions I always have to pursue with users. It gets a little frustrating at times, but unless you are relentless in getting the analysis correct, the end product will not be correct.

Not meaning to be nit-picky in my questions, but gotta know to do this right.

Also, I will be away from my computer for awhile today (it's Saturday), but will resume later.
It must 30 minute on both ends.
OK.Please let me know once your back I really need to sort this out ASAP
ASKER CERTIFIED SOLUTION
Avatar of dsacker
dsacker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SO far so good
Still having issue
Let's close this question out, and start a new one with the post of my code above. If you have modified my code above, post it instead. Also post good details of your issue.