我具有以下MySQL表的表结构
timestamp | agg_id | media_cost
1587121200 | 2 | 10
1587121200 | 3 | 5
1587121100 | 2 | 6
1587121200 | 2 | 2
1587341200 | 3 | 5
我必须每小时查询一次,以便它将昨天的成本与今天运行时的成本进行比较,并警告今天的成本是否小于昨天的成本的一半,并按agg_id分组。
我正在使用以下查询作为参考,但不确定如何获得预期的输出
select
today.datetime,
100 * (sum(today.cost) - sum(yesterday.cost))/sum(today.cost) as pctdiff,
sum(today.cost) as today,
sum(yesterday.cost) as yesterday
from (
select datetime, cost, id
from table
where datetime between curdate() and now() - interval 1 hour
) as today
join (
select datetime, cost, id
from table
where left(datetime,10) = curdate() - interval 1 day
) as yesterday on today.id=yesterday.id
group by today.datetime
order by today.datetime asc ;
有人可以在这里引导我