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

asked on

sql update

I am trying to make an update. If the session was cancelled I need to update the filed before that session to 1 and session that are after that session 2.

If i cancelling the session in the middle of the day I get what I need . but if I do the earliest session it marking as 1 when it should be 2

 /* -------------------------------------------------------------------- */
                /*  Lastly, find any adjoining sessions. These will be sessions within  */
                /*  30 minutes of the original cancelled session, where the car is the  */
                /*  same, but the instructor is different.                              */
                /* -------------------------------------------------------------------- */
              DECLARE @id tinyInt 
                SET @id = 0   
              UPDATE  a
                SET     a.adjoining = 1,
                        @id = a.hubLock = @id + 1 
                FROM    @Results a
                JOIN   (SELECT carKey, min(sessionStart) AS minStart, max(sessionEnd) AS maxEnd
                        FROM @Results
                        WHERE  1 IN (cancelled, contiguous)
                        GROUP BY carKey) c
                ON      c.carKey = a.carKey
                AND   ( a.sessionStart BETWEEN DATEADD(n, -29, maxEnd)   AND DATEADD(n, 29, maxEnd)
                    OR  a.sessionEnd   BETWEEN DATEADD(n, -29,minStart) AND DATEADD(n, 29, minStart) )

Open in new window

Avatar of Jim Horn
Jim Horn
Flag of United States of America image

You'll need to provide some sample data and expected return set for experts to answer this.
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada 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
erikTsomik, this question is abandoned or neglected?