我有下表数据:
我运行此查询:
select *, UNIX_TIMESTAMP(`time`) AS `timeu`, AVG(`value`) AS `valuea`
from `values`
group by `currency`, DATE(`time` - interval 7 day)
order by `id` asc
结果我得到了
正如您所看到的,id
是:1, 2, 3, 4, 11, 12
。我希望1, 2, 3, 4, 5, 6
- 我怎样才能简单地构建我的查询呢?
答案 0 :(得分:1)
你不会做group by
。
我怀疑你可以使用相关的子查询做你想做的事情:
select v.*, UNIX_TIMESTAMP(`time`) AS `timeu`,
(SELECT AVG(`value`)
from values v2
where v2.currency = v.currency and
v2.time >= v2.time - interval 7 day
) as `valuea`
from `values` v
order by `id` asc ;