0

此查询给出 1093 错误:

UPDATE values_table as outer_select 
    SET annual_change = sample_value - (
        SELECT sample_value FROM values_table 
        WHERE date_sampled = DATE_SUB(outer_select.date_sampled, INTERVAL 1 YEAR)
    );

我试图annual_change为每一行设置等于当前行sample_value减去去年的sample_value.

数据不回溯到开始时间,那么缺少历史值怎么办呢?

4

1 回答 1

1

尝试这个

UPDATE values_table as a
join  values_table as b 
ON b.date_sampled = DATE_SUB(a.date_sampled, INTERVAL 1 YEAR)
set a.annual_change = a.sample_value - b.sample_value
于 2013-04-27T23:02:21.057 回答