如何按日期分组并同时计算平均值

时间:2019-06-13 16:53:23

标签: date group-by mariadb

我对此很陌生,所以就这样:我正在尝试从unixtime转换为日期格式,然后按日期将其分组,同时在另一列上计算平均值。这是在MariaDB中。

CREATE OR REPLACE
VIEW `history_uint_view` AS select
    `history_uint`.`itemid` AS `itemid`,
    date(from_unixtime(`history_uint`.`clock`)) AS `mydate`,
    AVG(`history_uint`.`value`) AS `value`
from
    `history_uint`
where
((month(from_unixtime(`history_uint`.`clock`)) = month((now() - interval 1 month))) and ((`history_uint`.`value` in (1,
    0))
    and (`history_uint`.`itemid` in (54799, 54810, 54821, 54832, 54843, 54854, 54865, 54876, 54887, 54898, 54909, 54920, 58165, 58226, 59337, 59500, 59503, 59506, 60621, 60624, 60627, 60630, 60633, 60636, 60639, 60642, 60645, 60648, 60651, 60654, 60657, 60660, 60663, 60666, 60669, 60672, 60675, 60678, 60681, 60684, 60687, 60690, 60693, 60696, 60699, 64610)))

    GROUP by 'itemid', 'mydate', 'value'

1 个答案:

答案 0 :(得分:0)

如果您选择的聚合函数(如AVG)的列没有聚合函数,则应在GROUP BY子句中列出除具有聚合函数的列以外的所有列。

所以您的分组依据应该如下:

GROUP by itemid, mydate

如果使用单引号(例如'itemid'),MariaDB会将其视为字符串,而不是列。

相关问题