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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:44:29+00:00 2026-06-15T14:44:29+00:00

I’m really struggling creating a select statement that will return the record with the

  • 0

I’m really struggling creating a select statement that will return the record with the fastest time for each user.
If there is a tie on number if seconds, the lowest ‘clicks’ should be selected.

If I have the following records in my table

user    seconds     clicks
1       10.23       10
1       10.12       12
1       10.12       14
2       12.11       16
3       12.34       18
3       12.00       16

I need the lowest time and clicks for each user, so I need a query to return

user    seconds    clicks
1       10.12       12
2       12.11       16
3       12.00       16

The query I’ve tried is

select user, min(seconds), min(clicks) from mytable group by user

But it selects seconds 10.12 and clicks 10 for user 1 which is incorrect.
Can I do this in 1 query?

  • 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-15T14:44:30+00:00Added an answer on June 15, 2026 at 2:44 pm

    This is a variation of the greatest-n-per-group problem that comes up frequently.

    SELECT m.*
    FROM mytable AS m
    LEFT OUTER JOIN mytable AS m2 ON m.user = m2.user
      AND (m.seconds > m2.seconds OR m.seconds = m2.seconds AND m.clicks > m2.clicks)
    WHERE m2.user IS NULL;
    

    Explanation:

    We want m to be the row for each user that has the lowest seconds, or if there’s a tie for lowest seconds, the lowest clicks. In other words, there should be no other row for that user with a lower seconds or a lower clicks.

    Use an exclusion join trying to find any row with lower seconds or lower clicks. If no such row is found, then m is the one with the lowest values.


    It’s still possible to result in a tie and return more than one row for a user, if there’s more than one row with the least seconds and least clicks. In that case, you need to resolve the tie with some other column, hopefully one that is unique.

    Example:

    SELECT m.*
    FROM mytable AS m
    LEFT OUTER JOIN mytable AS m2 ON m.user = m2.user
      AND (m.seconds > m2.seconds OR m.seconds = m2.seconds AND (m.clicks > m2.clicks
      OR m.clicks = m2.clicks AND m.id > m2.id))
    WHERE m2.user IS NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

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.