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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:33:55+00:00 2026-06-17T20:33:55+00:00

I have a query that selects a set of Users. Each User can have

  • 0

I have a query that selects a set of Users. Each User can have a number
of Events associated with it. I want to join each User with the earliest Event
associated with that User (resulting in one row per User), and do so within a single query.

So, I kind of want to do this:

SELECT * FROM users
  left join (
    select * from events where events.user_id = users.id
    order by start_time limit 1) as event
    ON ("event"."user_id" = "users"."id")

but it is illegal to reference ‘users’ within the join’s select.

  • 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-06-17T20:33:56+00:00Added an answer on June 17, 2026 at 8:33 pm

    You can use a subquery to get the min(start_time) for each user_id. Then you will use this result to join back to the events table to get the details of the min event:

    SELECT * 
    FROM users u 
    LEFT JOIN 
    (
      SELECT Min(start_time) Min_Start, user_id 
      FROM events 
      GROUP BY user_id
    ) e1 
       ON u.id = e1.user_id 
    LEFT JOIN events e2 
     ON e1.user_id = e2.user_id 
     AND e1.min_start = e2.start_time 
    

    If you are using a database that has the ability to apply a row_number(), then you could use the following:

    select *
    from
    (
      SELECT *, 
        row_number() over(partition by e.user_id order by start_time) rn
      FROM users u 
      LEFT JOIN events e 
       ON u.id = e.user_id 
    ) src
    where rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Each of my user's have a set number of slots for storage. They can
I have several users that have items. I need each user to be able
I have many items and many users in a database. A user can set
I have a query that selects rows in a ListView without having a limit.
I have a query that basically combines tables of actions and selects from them
So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have a table that tracks user interactions with a customer’s record. I want
I have a SQL Server 2005 database that stores data for multiple users. Each
I have a table of items that are each individually associated with a category
We have a legacy website that several different users can log in to and

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.