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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:16:17+00:00 2026-05-14T06:16:17+00:00

I have 3 tables: users: Id Login 1 John 2 Bill 3 Jim computers:

  • 0

I have 3 tables:

users:

Id   Login
1    John
2    Bill
3    Jim

computers:

Id   Name
1    Computer1
2    Computer2
3    Computer3
4    Computer4
5    Computer5

sessions:

UserId   ComputerId   Minutes
1        2            47
2        1            32
1        4            15
2        5            5
1        2            7
1        1            40
2        5            31

I would like to display this resulting table:

Login   Total_sess   Total_min   Most_freq_computer   Sess_on_most_freq   Min_on_most_freq
John    4            109         Computer2            2                   54
Bill    3            68          Computer5            2                   36
Jim     -            -           -                    -                   -

Myself I can only cover first 3 columns with:

SELECT Login, COUNT(sessions.UserId), SUM(Minutes) FROM users
LEFT JOIN sessions
ON users.Id = sessions.UserId GROUP BY users.Id

And some kind of other columns with:

SELECT main.*
FROM (SELECT UserId, ComputerId, COUNT(*) AS cnt ,SUM(Minutes)
FROM sessions
GROUP BY UserId, ComputerId) AS main
INNER JOIN (
SELECT ComputerId, MAX(cnt) AS maxCnt FROM (
SELECT ComputerId, UserId, COUNT(*) AS cnt FROM sessions GROUP BY ComputerId, UserId
)
AS Counts GROUP BY ComputerId) 
                    AS maxes
ON main.ComputerId = maxes.ComputerId
AND main.cnt = maxes.maxCnt

But I need to get whole resulting table in one query. I feel I’m doing something completely wrong. Need help.

  • 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-14T06:16:17+00:00Added an answer on May 14, 2026 at 6:16 am

    Here you are:

    SELECT    u.login, t1.total_sess, t1.total_min, t2.mf, t2.sess_mf, t2.min_mf
    FROM      users u
    LEFT JOIN (
      SELECT   userid, COUNT(minutes) AS total_sess, SUM(minutes) AS total_min
      FROM     sessions
      GROUP BY userid
    ) AS t1 ON t1.userid = u.id
    LEFT JOIN (
      SELECT   userid, name AS mf, COUNT(*) AS sess_mf, SUM(minutes) AS min_mf
      FROM     sessions s
      JOIN     computers c ON c.id = s.computerid
      GROUP BY userid, computerid
      HAVING   COUNT(computerid) >= ALL(SELECT   COUNT(*)
                                        FROM     sessions s2
                                        WHERE    s2.userid = s.userid
                                        GROUP BY s2.computerid)
    ) AS t2 ON t2.userid = u.id
    

    I’m using MySQL syntax, but it should be pretty portable.

    If you need anything more, feel free to ask!

    EDIT: I updated the query, the previous one was wrong 🙁

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

Sidebar

Related Questions

For example I have 2 tables, Users and UserRelations , and it is a
Let's say you have two tables, Users and UserRoles. Here's how the two tables
I have a simple database with two tables. Users and Configurations. A user has
I have table with 50 entries (users with such details like Name Surname Location
I have a DataGridView binded to this table: [Users] ID Name -- ---- 11
I have 2 tables Users and Queries. They are connected via FK(UserId) in Queries
for example i have table users , which have 3 fields: id - login
For example have this table: Name | BodyType ---------------- John | 1 Ted |
I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares. if suppose, Users table having 6 user
I have a custom made web-site in Python/MySQL with custom user/session tables and 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.