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

LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
You'll need to provide some sample data and expected return set for experts to answer this.
David Johnson, CD, MVPOwnerCommented:
in your sql code you need to have a 'start transaction', if cancelled a 'rollback' and a 'commit'  if done

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
erikTsomik, this question is abandoned or neglected?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.