在 Mysql 中平均某些值

时间:2021-05-13 15:44:32

标签: mysql

我有一个表和一行,我从表中返回值

1

select ROUND(avg(corrente/1000*tensao*fpot/100/1000),2) as consumo,HOUR( hatual )as hora,date( hatual )as data 
from logsuc 
where nserlum=2009004991 
and date(hatual)>="2021-05-01" 
and date(hatual)<="2021-05-12" 
and corrente!=0 
group by hora,data;

这条线返回一块的平均值,效果很好,正如我们在图片中看到的那样

2

但是当我需要在同一张桌子上做不止一件的事情时,问题就来了

select ROUND(avg(corrente/1000*tensao*fpot/100/1000),2) as consumo,HOUR( hatual )as hora,date( hatual )as data 
from logsuc 
where ( nserlum=2009004986 or nserlum=2009004987) 
and date(hatual)>="2021-05-12" 
and date(hatual)<="2021-05-13" 
and corrente!=0 
group by hora,data;

他做了平均值,但会继续给出大约 0.10 的值,而不是 0.20 我需要一些东西,使两件每小时的平均值加起来等于那个小时的两件,然后给出 0.20,但我不知道如何要做到这一点。有没有办法做到这一点?谢谢

1 个答案:

答案 0 :(得分:1)

那么您还需要按 nserlum 分组:

select
    ROUND(avg(corrente / 1000 * tensao * fpot / 100 / 1000), 2) as consumo,
    HOUR(hatual) as hora,
    date(hatual) as data
from logsuc 
where (nserlum=2009004986 or nserlum=2009004987) 
    and date(hatual) >= "2021-05-12"
    and date(hatual) <= "2021-05-13"
    and corrente != 0
group by hora,data,nserlum;