0

Im using MySQL server v5.8 as database, how to find median price in query like this:

 price_query = Product.objects \
                        .filter(price_query) \
                        .annotate(dt=Trunc('StartDate', frequency)) \
                        .values('dt') \
                        .annotate(avg_price=Avg('Price'), std_price=StdDev('Price'),
                         count=Count('Price'), max_price=Max('Price'),
                         min_price=Min('Price'), median='???') \
                         .order_by('dt')

response is look like this

{"date":"2021-05-01T00:00:00Z","avg_price":4326.666666666667,"std_price":20.548046676563168,"min_price":4300.0, "max_price":4350.0,"count":3}, {...}, {...}

Any help is highly appreciated.

party911
  • 47
  • 5
  • Which version of MySQL is this? Depending on the version, calculating the median might be... involved. https://stackoverflow.com/questions/1291152/simple-way-to-calculate-median-with-mysql – AKX Dec 01 '21 at 05:58
  • yap i saw it. but how i implement this in django, not as rawsql. If it possible – party911 Dec 01 '21 at 07:34
  • @party911 did you figure this out? – fmakawa Sep 12 '22 at 18:56

0 Answers0