0

I'm going to convert this sql to django commands:

SELECT
  core.id,
  core.title,
  core.age_id,
  core.cat_id,
  max(date) AS max_date
FROM core
WHERE core.state = 'ABC'
GROUP BY cat_id, age_id

I tried this, but not works correctly:

Core.objects.values('id', 'title', 'age_id', 'cat_id').filter(state='ABC').annotate(
            max_date=Max('date')).aggregate(Count('age_id', 'cat_id'))
1
  • Why are you using aggregate? Commented Dec 4, 2016 at 7:54

1 Answer 1

1

You have to do this with values() but restrict fields that you want to group by

Core.objects.values('age_id', 'cat_id').filter(state='ABC').annotate(Max('date'), Count('age_id', 'cat_id'))
Sign up to request clarification or add additional context in comments.

6 Comments

I have compile error in Count('age_id', 'cat_id')
Positional argument after keyword argument
Edited answer, try that
I need id and title columns too.
That is syntaz for generatinf query you wanted. About result, 1. explain more what you trying to do? 2. What is the result you are expecting?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.