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

  • Home
  • SEARCH
  • 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 277111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:01:28+00:00 2026-05-12T01:01:28+00:00

I have a table users which has a primary key userid and a datetime

  • 0

I have a table users which has a primary key userid and a datetime column pay_date.

I’ve also got a table user_actions which references users via the column userid, and a datetime column action_date.

I want to join the two tables together, fetching only the earliest action from the user_actions table which has an action_date later than or equal to pay_date.

I’m trying things like:

select users.userid from users
left join user_actions on user_actions.userid = users.userid
where user_actions.action_date >= users.pay_date
order by user_actions.pay_date

But obviously that returns me multiple rows per user (one for every user action occurring on or after pay_date). Any idea where to go from here?

Apologies for what probably seems like a simple question, I’m fairly new to t-sql.

  • 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-12T01:01:29+00:00Added an answer on May 12, 2026 at 1:01 am

    If you have a PRIMARY KEY on user_actions:

    SELECT  u.*, ua.*
    FROM    users u
    LEFT JOIN
            user_actions ua
    ON      user_actions.id = 
            (
            SELECT  TOP 1 id
            FROM    user_actions uai
            WHERE   uai.userid = u.userid
                    AND uai.action_date >= u.pay_date
            ORDER BY
                    uai.action_date
            )
    

    If you don’t:

    WITH    j AS
            (
            SELECT  u.*, ua.*, ROW_NUMBER() OVER (PARTITION BY ua.userid ORDER BY ua.action_date) AS rn, ua.action_date
            FROM    users u
            LEFT JOIN
                    user_actions ua
            ON      ua.userid = u.userid
                    AND ua.action_date >= u.pay_date
            )
    SELECT  *
    FROM    j
    WHERE   rn = 1 or action_date is null
    

    Update:

    CROSS APPLY proposed by @AlexKuznetsov is more elegant and efficient.

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

Sidebar

Ask A Question

Stats

  • Questions 225k
  • Answers 225k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since you want CashTransferCurrencyAmount instance to know which collection it… May 13, 2026 at 12:59 am
  • Editorial Team
    Editorial Team added an answer Your layout is a bit interesting. Instead of using empty… May 13, 2026 at 12:59 am
  • Editorial Team
    Editorial Team added an answer If you add the lines into a Set (as the… May 13, 2026 at 12:59 am

Related Questions

I have a table with sets of settings for users, it has the following
I have a DB table in which each row has a randomly generated primary
I have a table called users with roughly 250,000 records in it. I have
I need a user interface that allows users to sort a table according to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.