希望从常规数据输入中插入当月不存在的平均值。当前年份值不应插入到reg_data3表中的表avg_month_val1。
INSERT INTO
`clima_data`.`avg_month_val1` (
`year` ,
`month` ,
`evep` ,
`sunshine_hrs` ,
`rainfall` ,
`max_temp` ,
`min_temp` )
SELECT
year(str_to_date(date, '%m/%d/%Y'))as year,
month(str_to_date(date, '%m/%d/%Y'))as month,
round(avg(evep)),
round(Avg(sunshine_hrs)),
round(sum(rainfall)),
round(AVG(max_temp)),
round(avg(min_temp))
FROM
reg_data
GROUP BY
year(str_to_date(date, '%m/%d/%Y')),
month(str_to_date(date, '%m/%d/%Y'));
答案 0 :(得分:0)
您应该写一个WHERE
条件来排除当前年份。如果您只想要过去几年的数据,则可以将!=
运算符更改为<
。
另一点:date是mysql的DATE()
函数的保留字。所以你应该为date
和
`date`
您可能的解决方案如下:
INSERT INTO
`clima_data`.`avg_month_val1` (
`year` ,
`month` ,
`evep` ,
`sunshine_hrs` ,
`rainfall` ,
`max_temp` ,
`min_temp` )
SELECT
YEAR(STR_TO_DATE(`date`, '%m/%d/%Y'))as year,
YEAR(STR_TO_DATE(`date`, '%m/%d/%Y'))as month,
ROUND(AVG(evep)),
ROUND(AVG(sunshine_hrs)),
ROUND(SUM(rainfall)),
ROUND(AVG(max_temp)),
ROUND(AVG(min_temp))
FROM
reg_data
WHERE
YEAR(STR_TO_DATE(date, '%m/%d/%Y')) != YEAR(CURDATE())
GROUP BY
YEAR(STR_TO_DATE(`date`, '%m/%d/%Y')),
MONTH(STR_TO_DATE(`date`, '%m/%d/%Y'));