V
V
Vladimir Kuts2018-06-09 12:22:39
Django
Vladimir Kuts, 2018-06-09 12:22:39

To receive by one request a difference of two last values ​​in selection?

A selection is made

res = SomeModel.objects.filter(user=someuser).order_by('somedate').values_list('somevalue', flat=True)

The result of which will be an array of values.
How can I get the difference of the last two values ​​in one query? Considering that there can be less than two values ​​(then display 0)
Something like res[-1] - res[-2] - only at the level of database queries

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2018-06-09
@vintello

two cursors at the DB level. All this should be done in the form of a stored procedure.
I don't know any other option

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question