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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:45:39+00:00 2026-05-12T16:45:39+00:00

I have a table of user access sessions which records website visitor activity: accessid,

  • 0

I have a table of user access sessions which records website visitor activity:

accessid, userid, date, time, url

I’m trying to retrieve all distinct sessions for userid 1234, as well as the earliest date and time for each of those distinct sessions.

SELECT 
    DISTINCT accessid, 
    date, 
    time 
FROM 
    accesslog 
WHERE userid = '1234' 
GROUP BY accessid

This gives me the date and time of a random row within each distinct accessid. I’ve read a number of posts recommending the use of min() and max(), so I tried:

SELECT DISTINCT accessid, MIN(DATE) AS date, MIN(TIME) AS time FROM accesslog WHERE userid = '1234' GROUP BY accessid ORDER BY date DESC, time DESC

… and even…

SELECT DISTINCT accessid, MIN(CONCAT(DATE, ' ', TIME)) AS datetime FROM accesslog WHERE userid = '1234' GROUP BY accessid ORDER BY date DESC, time DESC

… but I never get the correct result of the earliest date and time.

What is the trick to ordering this kind of query?

EDIT –

Something weird is happening….

The code posted below by Bill Karwin correctly retrieves the earliest date and time for sessions that started in 2009-09. But, for sessions that began on some day in 2009-08, the time and date for the first hit occurring in the current month is what is returned. In other words, the query does not appear to be spanning months!

Example data set:

accessid    | userid    | date          | time
1           | 1234      | 2009-08-15    | 01:01:01
1           | 1234      | 2009-09-01    | 12:01:01
1           | 1234      | 2009-09-15    | 13:01:01
2           | 1234      | 2009-09-01    | 14:01:01
2           | 1234      | 2009-09-15    | 15:01:01

At least on my actual data table, the query posted below finds the follow earliest date and time for each of the two accessid’s:

accessid    | userid    | date          | time
1           | 1234      | 2009-09-01    | 12:01:01
2           | 1234      | 2009-09-01    | 14:01:01

… and I would guess that the only reason the result for accessid 2 appears correct is because it has no hits in a previous month.

Am I going crazy?

EDIT 2 –

The answer is yes, I am going crazy. The query works on the above sample data when placed in a table of duplicate structure.

Here is the (truncated) original data. I included the very first hit, another hit in the same month, the first hit of the next month, and then the last hit of the month. The original data set has many more hits in between these points, for a total of 462 rows.

accessid                            | date          | time
cbb82c08d3103e721a1cf0c3f765a842    | 2009-08-18    | 04:01:42
cbb82c08d3103e721a1cf0c3f765a842    | 2009-08-23    | 23:18:52
cbb82c08d3103e721a1cf0c3f765a842    | 2009-09-17    | 05:12:16
cbb82c08d3103e721a1cf0c3f765a842    | 2009-09-18    | 06:29:59

… the query returns the 2009-09-17 value as the earliest value when the original table is queried. But, when I copy the …….. oh, balls.

It’s because the hits from 2009-08% have an empty userid field.

  • 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-12T16:45:39+00:00Added an answer on May 12, 2026 at 4:45 pm

    This is a variation of the “greatest-n-per-group” problem that comes up on StackOverflow several times per week.

    SELECT 
            a1.accessid, 
            a1.date, 
            a1.time 
    FROM 
            accesslog a1
    LEFT OUTER JOIN
            accesslog a2
      ON (a1.accessid = a2.accessid AND a1.userid = a2.userid
        AND (a1.date > a2.date OR a1.date = a2.date AND a1.time > a2.time))
    WHERE a1.userid = '1234'
      AND a2.accessid IS NULL;
    

    The way this works is that we try to find a row (a2) that has the same accessid and userid, and an earlier date or time than the row a1. When we can’t find an earlier row, then a1 must be the earliest row.


    Re your comment, I just tried it with the sample data you provided. Here’s what I get:

    +----------+------------+----------+
    | accessid | date       | time     |
    +----------+------------+----------+
    |        1 | 2009-08-15 | 01:01:01 | 
    |        2 | 2009-09-01 | 14:01:01 | 
    +----------+------------+----------+
    

    I’m using MySQL 5.0.75 on Mac OS X.

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

Sidebar

Related Questions

I have a table User which has the fields (id, first_name, middle_name, last_name). I
I have one table containing user sessions and another to indicate violations in the
I have a table (user) that contains user information. I have another table (userview)
I have this table: User id INT PK login VARCHAR UNIQUE I want to
I have a table User and another Person , a person is an user
I have a table with user items. Each user may have several types of
I have a table like so: Table eventlog user | user_group | event_date |
I'm just learning ruby on rails and I have a table of user roles
I have a User table that has all of their avatars saved in an
We have a website (foo.com) that does online training. A user logs in, then

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.