How to write this raw sql subquery as native django query?

I have worked on SQL for 3 years when I used .Net technology, And I liked SQL command, Really it’s magnificent. But I gave me chance to penetrate ORM you will get more robust and secure query by it.

I think this query is not difficult to be a result of ORM,
The Left Outer Join is more difficult but I can make it by ORM.
I have no ORM query for your SQL query now.
But If you think well, and give yourself another chance to try ORM you will have benefits.
You can use(filter, values, values_list, select_related, preftch_related, annotate and aggregate with order_by and distinct) then you get what you want .
Try to print the ORM query to the console by (orm_qs.query) to see the SQL equivalent,
Give us your models and you will get what you want …
Also when using ORM you can use(Case, When and etc) of SQL commands
Take a look at