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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:16:47+00:00 2026-05-28T19:16:47+00:00

I have a USERS table. Each user has connections in a CONNECTIONS table. Each

  • 0

I have a USERS table.

Each user has connections in a CONNECTIONS table.

Each connection has a datetime and some referenced properties like timezone, stored in a TZ reference table.

I’d like to select the userID, and the TimeZoneLabel for the first and the last connection. Even if a user has no connection (so NULL or anything else would be displayed)

Do something like :

Select USERS.id,
min(TZ.label),
max(TZ.label)

from USERS
join CONNECTION on USERS.id = CONNECTIONS.userid
join TZ on TZ.id = CONNECTIONS.tzid

group by USERS.id
order by max(CONNECTIONS.dateconn)

But I can’t achieve doing that. I’ve found articles on the net about that, but nothing works when I try. The example above does not work for the label, as there are no real min / max values but the one used on the first CONNECTION and the one used on the last one.

And I have many of these in my real request so I’d like to avoid too many sub-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-05-28T19:16:49+00:00Added an answer on May 28, 2026 at 7:16 pm

    Without the timezones:

    SELECT 
           u.id             AS userId
         , MIN(c.dateconn)  AS firstConnectionDatetime
         , MAX(c.dateconn)  AS lastConnectionDateTime
    FROM Users AS u
      LEFT JOIN Connection AS c
        ON u.id = c.userid    
    GROUP BY u.id    
    ORDER BY lastConnectionDateTime
    

    With timezones (assuming Connection table has id as Primary Key):

    SELECT 
           u.id             AS userId
         , ConMin.dateconn  AS firstConnectionDatetime
         , ConMax.dateconn  AS lastConnectionDateTime
         , TzMin.label      AS firstTimeZoneLabel
         , TzMax.label      AS lastTimeZoneLabel
    FROM Users AS u
      LEFT JOIN Connection AS ConMax
        ON ConMax.id =
            ( SELECT c.id
              FROM Connection AS c
              WHERE u.id = c.userid 
              ORDER BY c.dateconn DESC
              LIMIT 1
            )
      LEFT JOIN TzMax
        ON TzMax.id = ConMax.tzid
      LEFT JOIN Connection AS ConMin
        ON ConMin.id =
            ( SELECT c.id
              FROM Connection AS c
              WHERE u.id = c.userid 
              ORDER BY c.dateconn ASC
              LIMIT 1
            )
      LEFT JOIN TzMin
        ON TzMin.id = ConMin.tzid
    

    A compound (userid, dateconn, id) index on Connection table would help performance.

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

Sidebar

Related Questions

I have a table of users and each user has another user that they
I have a table users which has a primary key userid and a datetime
I have two tables. One is a 'Users' table. Each row (user) in the
I have a table with user items. Each user may have several types of
I have an application which has a Projects table and a Users table. In
We have an application which stores its data in SQL Server. Each table has
Let's say I have 10 books, each book has assigned some categories (ex. :php,
I have a site that allows users to vote on images. Each image has
I have users table. There are three other tables: developers, managers, testers. All of
i have users table and i have posts table i want select from users

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.