2

I have a table that looks a bit like this:

   date   |  timestamp  |  predicted_value
2017-10-25| 21758492893 |       0.4
2017-10-25| 21758492917 |       0.3
2017-10-25| 21758493210 |       0.4
2017-10-25| 21758493782 |       0.2
2017-10-25| 21758494903 |       0.1
2017-10-26| 21758492893 |       7.2
               ....

I'd like to be able to use a single query to get the latest predicted_value for a given date, by timestamp. I.E in my output I want:

   date   |  timestamp  |  predicted_value
2017-10-25| 21758494903 |       0.1
2017-10-26| 21758494903 |       7.7
               ....

I think it should go something like this:

Predictions.objects.all().order_by(
    '-timestamp'
).values(
    'date'
).annotate(
    prediction=F('predicted_value')
)

Is this going to work? Will F select the first value found in the aggregation or can I not use it that way?

user31415629
  • 925
  • 6
  • 25

0 Answers0