0

This is the image of the table and output required with details

So, I wrote a Django query

ModelClassName.objects.values('houseid','vehid').annotate(Count('vehid')).order_by('houseid')

This is giving me the count of each vehid but as I am not sure how to incorporate Max here, I am unable to get the right result. Any help is appreciable. Thanks.

shelholmes221
  • 518
  • 1
  • 7
  • 18

1 Answers1

0

Create a view to get the counts:

SELECT HOUSEID, VEHID, COUNT(*) FROM TABLE GROUP BY HOUSEID, VEHID

Then query the View

Select Houseid, VEHID, COUNT from MYVIEW A where A.COUNT = (Select Max(B.count) FROM MYVIEW B WHERE A.HOUSEID = B.HOUSEID) 

Then you can use Django SQL (https://docs.djangoproject.com/en/2.0/topics/db/sql/) to help you.

(test and let me know if that worked)

Walucas
  • 2,549
  • 1
  • 21
  • 44