想要选择每个月的每日价值的平均值

时间:2013-11-14 13:58:34

标签: mysql

我有表reg_data 3我每天输入数据一个值。我想选择该值的平均值(值的总和/天的数量)但是我需要sumation,降雨量为一个月.....但是这段代码不能正常工作。

SELECT 
    year(str_to_date(date, '%m/%d/%Y'))as year,
    month(str_to_date(date, '%m/%d/%Y'))as month,
    Avg(sunshine_hrs) as sunshine_hrs,
    AVG(max_temp) as max_temp
    sum(rainfall) as rainfall
    avg(evep) as evep 
    avg(min_temp) as min_temp
FROM
    reg_data3
GROUP BY
    year(str_to_date(date, '%m/%d/%Y')),
    month(str_to_date(date, '%m/%d/%Y'));

CREATE TABLE `reg_data3` (
 `date` varchar(10) NOT NULL default '',
 `time` time NOT NULL,
 `rainfall` float default NULL,
 `evep` float default NULL,
 `max_temp` float default NULL,
 `min_temp` float default NULL,
 `sunshine_hrs` float default NULL,
 PRIMARY KEY  (`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

1 个答案:

答案 0 :(得分:0)

您需要在查询中指定的每个字段之后添加逗号,但最后一个字段除外。像这样:

SELECT 
    year(str_to_date(date, '%m/%d/%Y'))as year,
    month(str_to_date(date, '%m/%d/%Y'))as month,
    Avg(sunshine_hrs) as sunshine_hrs,
    AVG(max_temp) as max_temp,
    sum(rainfall) as rainfall,
    avg(evep) as evep,
    avg(min_temp) as min_temp
FROM
    reg_data3
GROUP BY
    year(str_to_date(date, '%m/%d/%Y')),
    month(str_to_date(date, '%m/%d/%Y'));