简体   繁体   English

在SQL SELECT中找到COUNT个最大值

[英]Find MAX of COUNT in SQL SELECT

I have SQL table information of runs like time, distance, pace ... I would like to have information of the best maximum runned distance by the month by user. 我有SQL表的运行信息,例如时间,距离,步速...我想按用户按月获得最佳最大运行距离的信息。 I need to COUNT runned distance by every month and find the best one (MAX). 我需要每个月计算一次跑步距离,并找到最佳距离(最大)。

My table looks like this: 我的桌子看起来像这样:

+----+--------+--------+----------+
| ID | USERID | MONTH  | DISTANCE |
+----+--------+--------+----------+
| 1  |    1   | 201707 |   6.69   |
+----+--------+--------+----------+
| 2  |    2   | 201707 |   6.03   |
+----+--------+--------+----------+
| 3  |    2   | 201707 |   6.02   |
+----+--------+--------+----------+
| 4  |    1   | 201706 |   7.25   |
+----+--------+--------+----------+
| 5  |    1   | 201706 |   7.13   |
+----+--------+--------+----------+
| 6  |    2   | 201705 |   7.03   |
+----+--------+--------+----------+
| 7  |    3   | 201705 |   6.31   |
+----+--------+--------+----------+
| 8  |    1   | 201705 |   5.28   |
+----+--------+--------+----------+
| 9  |    1   | 201704 |   4.44   |
+----+--------+--------+----------+
| 10 |    2   | 201704 |   5.94   |
+----+--------+--------+----------+

I try this but I have no output: 我尝试这样做,但没有输出:

$select3run = mysqli_query($conn, "SELECT month, MAX(mycount) FROM (SELECT month, SUM(distance) AS mycount FROM runbeh GROUP BY month, distance) a GROUP BY month");
while($data=mysqli_fetch_array($select3run)) {
$max=$data['mycount'];
}

echo $max;

Have no more idea how to find value I'm looking for. 不知道如何寻找我想要的价值。 Can somebody help me? 有人可以帮我吗?

You should use a proper alias for the column name eg: my_max 您应该为列名使用适当的别名,例如: my_max

    $select3run = mysqli_query($conn, "
        SELECT month, MAX(mycount)  as my_max
        FROM (SELECT month, SUM(distance) AS mycount 
        FROM runbeh GROUP BY month, distance) a 
        GROUP BY month");
    while($data=mysqli_fetch_array($select3run)) {
      $max=$data['my_max'];
    }

    echo $max;

Example: 例:

http://sqlfiddle.com/#!9/29009f/8 http://sqlfiddle.com/#!9/29009f/8

SQL: SQL:

SELECT r.userid, r.month, max(r.sumdistance) 
FROM
(
  SELECT userid, month, sum(distance) as sumdistance 
  FROM runners 
  GROUP BY month, userid
) as r 
GROUP BY r.userid

SOLVED! 解决了!

Many thanks to Raimondas Kazlauskas and scaisEdge whos show me the way and help me. 非常感谢Raimondas Kazlauskas和scaisEdge,他们为我提供了帮助。

Working code for show most distance runned per month by users: 显示用户每月运行的最远距离的工作代码:

$select3run = mysqli_query($conn, "SELECT month, MAX(mycount)  as my_max 
FROM
(SELECT month, SUM(distance) AS mycount FROM runners GROUP BY month) a GROUP 
BY mycount DESC LIMIT 1");
while($data=mysqli_fetch_array($select3run)) {
$max=$data['my_max'];
echo $max; }

Where $max is the biggest SUM of distance which was runned by one month by user. 其中$ max是距离最大的总和,由用户运行一个月。

You should Try this one :- 你应该试试这个:-

Best run by every month of every user :- 最好由每个用户的每月运行:-

select max(distance) AS MaxDistance,userid from runbeh group by month,userid 选择max(distance)AS MaxDistance,按月从runbeh组中选择用户标识,用户标识

Note :- Never put any aggregate function in Group BY clause 注意:-切勿在Group BY子句中放置任何聚合函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM