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 6158199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:55:02+00:00 2026-05-23T20:55:02+00:00

The basic gist of my issue is, for every event A, I need to

  • 0

The basic gist of my issue is, for every event A, I need to find the earliest following event B that’s associated with the same user. Currently, I have:

SELECT e.UserID, e.date, min(e2.date) 
FROM Event e INNER JOIN 
    Event e2 ON e.UserID = e2.UserID AND e.date <= e2.date
WHERE e.Event LIKE 'A' AND e2.Event LIKE 'B'

However, for every event A (which can happen for a user any number of times), numerous event B’s happen, so the inner join is creating numerous extra rows that it then has to weed through on the min function. Is there a more efficient/faster way of doing this?

(the server is MSSQL Server 2008)

UPDATE:
Would it be faster with Rank()?

Select UserID, date, date2 
from (
    Select e.UserID, e.date, e2.date as date2, rank() OVER (PARTITION BY e.date, e.UserID ORDER BY e2.date) as rank
    FROM Event e INNER JOIN Event e2 on e.UserID = e2.UserID
    WHERE e.Event = 'A' and e2.Event = 'B' and e.date <= e2.date
)
WHERE rank = 1

Or will optimization bring them out to basically equivalent?

  • 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-23T20:55:02+00:00Added an answer on May 23, 2026 at 8:55 pm

    Is it faster to join a third time, like this? Probably not, but it might be worth trying. Here any data returned in table “e3” represent dates inbetween the e date and the e2 date. So we left join with that and grab the NULL values.

    SELECT e.UserID, e.date, e2.date 
    FROM Event e 
    INNER JOIN Event e2 ON (e.UserID = e2.UserID AND e.date <= e2.date)
     LEFT JOIN Event e3 ON (e.UserID = e3.UserID AND e.date <= e3.date AND e3.date <= e2.date AND e3.Event = 'B')
    WHERE e.Event = 'A' AND e2.Event = 'B'
    AND e3.date IS NULL
    

    I am thinking this probably uses the same strategy as your MIN query, but maybe not? I’m curious to know either way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic premise: I have a Room which publishes an event when an Avatar enters
I have managed to run a basic rails app1 on App Engine using: http://gist.github.com/268192
basic overview... i have a site setup in iis... - mysite (wwwroot\mysite) under that
So the gist is that I need to post XML data query to a
I have a website (the basic gist of which is described in this question
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
I did some searching and didn't find a question that directly answered this question.
This is a convoluted example so bear with me: I have a script that
Alright, Here's the gist of what I'm planning to do. I'm going to have
Basic need is if a record has an Attribute of Urgent, then the attributevalue

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.