Code: Select all
class Price:
timestamp = models.IntegerField()
price = models.FloatField()
Code: Select all
queryset = (
Price.objects.annotate(timestamp_agg=Floor(F('timestamp') / 3600))
.values('timestamp_agg')
.annotate(
timestamp=Min('timestamp'),
high=Max('price'),
)
.values('timestamp', 'high')
.order_by('timestamp')
)
Code: Select all
select min(timestamp) timestamp, max(price) high
from core_price
group by floor((timestamp / 3600))
order by timestamp
Code: Select all
select *, avg(high) over (order by timestamp rows between 4 preceding and current row) ma
from (select min(timestamp) timestamp, max(price) high
from core_price
group by floor((timestamp / 3600))
order by timestamp)
Code: Select all
Window(expression=Avg('high'), frame=RowRange(start=-4, end=0))
Code: Select all
>>> queryset.annotate(ma=Window(expression=Avg('high'), frame=RowRange(start=-4, end=0)))
django.core.exceptions.FieldError: Cannot compute Avg('high'): 'high' is an aggregate
Mobile version