最大连接数

发布于 2024-08-28 07:59:58 字数 1546 浏览 2 评论 0原文

我有 3 个表:

用户:

Id   Login
1    John
2    Bill
3    Jim

计算机:

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

会话:

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

我想显示此结果表:

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     -            -           -                    -                   -

我自己只能覆盖前 3 列:

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

以及其他某种列:

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

但我需要在一个查询中获取整个结果表。我觉得我正在做一些完全错误的事情。需要帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-09-04 07:59:58

在这里:

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

我正在使用 MySQL 语法,但它应该非常便携。

如果您需要更多信息,请随时询问!

编辑:我更新了查询,前一个是错误的:(

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 :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文