Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:33:33+00:00 2026-05-14T04:33:33+00:00

I have a table as follows > RowID SessionID EventID RequestedURL Date > 1

  • 0

I have a table as follows

> RowID   SessionID       EventID  RequestedURL   Date
> 1       m2jqyc45g       1        Start          24/03/2010 19:52
> 2       m2jqyc45g       1        ProductPage    24/03/2010 19:52
> 3       m2jqyc45g       28       BuyNow         24/03/2010 19:52
> 4       tjmvb55dc4dg    1        ProductPage    24/03/2010 19:52
> 5       tjmvb55dc4dg    1        BuyNow         24/03/2010 19:56
> 6       tjmvb55dc4dg    1        Clicked OK     24/03/2010 19:56
> 7       m2jqyc45g       1        Clicked OK     24/03/2010 19:56
> 8       tjmvb55dc4dg    28       Help           24/03/2010 19:56
> 9       m2jqyc45g       1        Home           24/03/2010 19:56
> 6       m2jqyc45g       1        ProductPage    24/03/2010 19:56
> 7       tjmvb55dc4dg    1        BuyNow         24/03/2010 19:56
> 8       tjmvb55dc4dg    28       Clicked OK     24/03/2010 19:56
> 9       tjmvb55dc4dg    1        Home           24/03/2010 19:56

How do I write a query that does a count whenever the rows BuyNow and Clicked OK have been recorded consecutively in a particular session?
For example, the dataset above shows 2 users on the site recording separate sessions. The return count should be 3.

I’m running SQL Server 2008

EDIT
Just to clarify What I mean by consecutive rows and why the count should return 3. In the session m2jqyc45g, the BuyNow and Clicked Ok occurs consecutive within the session but when it’s entered into the table, there are other concurrent sessions too which breaks the sequence. If you were to order the result set by SessionID and then by Date, you will get them one after the other. Also, there was a typo with RowID7, ProductPage should be BuyNow. Sorry for that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:33:33+00:00Added an answer on May 14, 2026 at 4:33 am
    SELECT COUNT(*)
    FROM yourTable y
    WHERE RequestedURL = 'BuyNow'
        AND Exists (SELECT * 
                 FROM yourTable x 
                 WHERE x.RowID = (SELECT MIN(RowID) 
                                  FROM yourTable z 
                                  WHERE z.SessionID = y.SessionID
                                   AND z.RowID > y.RowID)
                     AND RequestedURL = 'Clicked')
    

    This should do it. There might be a faster way but I am not sure what else you can do. Also, you will probably want to but indexes on RowID and SessionID.

    Your other option is to use Common Table Expressions to get row IDs that are group by session and time. The below could should do that for you.

    WITH temp AS (
    SELECT
        SessionID,
        RequestedURL,
        [Date],
        RN = ROW_NUMBER() OVER (ORDER BY SessionID,[Date])
    FROM yourTable
    ORDER BY SessionID,[Date])
    SELECT
        COUNT(*)
    FROM temp x
        JOIN temp y ON x.SessionID = y.SessionID
            AND x.RN = y.RN - 1
    WHERE x.RequestedURL = 'BuyNow'
        AND y.RequestedURL = 'Clicked'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.