I'm having trouble transferring my query to django. In sqlite3 it looked like this:
SELECT A, MIN(B), MAX(B) from table GROUP BY A
This would output unique values from A with a range of values from B Any hints on how to approach this? Is it even possible in django?
You can use values()
for the GROUP BY
, and annotate()
for the MIN
and MAX
:
from django.db.models import Min, Max
MyModel.objects.values('A').annotate(min_b=Min('B'), max_b=Max('B'))
You'll get a list of dictionaries, containing the keys A
, min_b
and max_b
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With