获取查询集中的最新值?

时间:2017-10-25 13:06:50

标签: django django-queryset

我有一张看起来有点像这样的表:

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

我希望能够使用单个查询按时间戳获取给定日期的最新predict_value。 I.E在我的输出中我想要:

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

我认为它应该是这样的:

F

这会起作用吗? Public Function copydata(forma As Form, i As Integer) Dim dtb As DAO.Database Dim tabl As Recordset Dim flag As Boolean Set dtb = CurrentDb Set tabl = dtb.OpenRecordset("SamplesProcessing") forma.Refresh With tabl Do Until flag = True .MoveFirst Do Until .EOF If !SampleN = i Then flag = True .Edit Select Case forma!Procedure Case 1 'Before treatment ![W of raw sample] = forma!SampleMass ![ChemTreat operator] = forma!UserName Case 2 'Fraction ![W of fraction used] = forma!SampleMass ![ChemTreat operator] = forma!UserName Case 3 'After first burnng ![W after opened carbonisation] = forma!SampleMass ![ChemTreat operator] = forma!UserName Case 4 'After second burning ![W after closed carbonisation] = forma!SampleMass ![ChemTreat operator] = forma!UserName Case 5 'After chem treatment ![W after chem treatment] = forma!SampleMass ![ChemTreat operator] = forma!UserName ![HCl treatment] = True Case 6 'Combustion ![W of C for reaction] = forma!SampleMass ![W of Li for reaction] = forma!LiMass ![Synthesis operator] = forma!UserName Case 7 'Distillating ![W of BenzeneAndScint] = forma!BenzeneMass ![PercentofBenzene] = forma!BenzenePercent ![Distilaltion operator] = forma!UserName Case 8 'Synthesis ![Synthesis operator] = forma!UserName Case 9 'H2SO4 treatment ![H2SO4 treatment] = True Case 10 'HCl treatment ![HCl treatment] = True Case 11 'KOH treatment ![KOH treatment] = True End Select If IsNull(forma![ProcDate]) = False Then !LastOperDate = forma![ProcDate] End If If IsNull(forma!Comments) = False Then If IsNull(!Comments) = True Then !Comments = forma!Comments Else !Comments = !Comments + "; " + forma!Comments End If End If .Update If .EOF = True Then flag = False End If End If .MoveNext Loop If flag = False Then .AddNew !SampleN = i .Update End If Loop End With Set tabl = Nothing End Function 会选择聚合中找到的第一个值,还是不能以这种方式使用它?

0 个答案:

没有答案