两个值的除法之和,分组,最高三个

时间:2017-02-26 07:12:02

标签: mysql group-by sum

我想从表中获得最高的三个值。通过将按名称分组的所有“伏特”加起来然后除以安培来计算该值。每个名称的放大器总是相同的,因此不需要计算(也不需要平均)。

我尝试过以下示例,但TotalSum为所有三个返回0.

paint.setStrokeWidth(1);
paint.setStyle(SkPaint::kStroke_Style);
paint.setAntiAlias(true);
canvas.drawRect(skrect,paint);  //draw up rect

skrect.fTop += 110;
skrect.fBottom += 110;

paint.setAntiAlias(false);
canvas.drawRect(skrect, paint); //draw down rect

表:

SELECT name, sum(volts/amps) TotalSum 
FROM mytable 
WHERE MONTH(FROM_UNIXTIME(time_to)) = MONTH(CURRENT_DATE()) 
GROUP BY field1 
ORDER BY TotalSum 
LIMIT 3

...

while ($row = mysqli_fetch_array($mostused, MYSQLI_ASSOC)) {
    echo $row['name'] . ' (' . $row['TotalSum'] . '), ';
}

按最高排序的结果:

name    volts    amps
A       10       2
A       15       2
A       12       2
B       2        1
B       4        1
B       8        1
C       20       5
C       20       5
C       20       5

我的查询中有什么错误?

1 个答案:

答案 0 :(得分:0)

按降序排序以获得最高的三个:

SELECT name, sum(volts/amps) TotalSum 
FROM mytable 
WHERE MONTH(FROM_UNIXTIME(time_to)) = MONTH(CURRENT_DATE()) 
GROUP BY name 
ORDER BY TotalSum desc  -- here
LIMIT 3