ofey ofey - 5 months ago 18
SQL Question

Django placing of WHERE in raw SQL query

The order of the WHERE in this query was wrong, but it is correct now.

postModel = list(PostModel.objects.raw(
SELECT max(pub_date),
count(topic_id) AS freq,
count(DISTINCT author) AS contributors
FROM crudapp_postmodel
WHERE author = "art"
GROUP BY topic_id
ORDER BY pub_date DESC
))


Thanks

Answer

Simple:

SELECT max(pub_date), 
       count(topic_id) AS freq, 
       count(DISTINCT author) AS contributors 
       FROM crudapp_postmodel 
       WHERE author = "art" 
       GROUP BY topic_id 
       ORDER BY pub_date DESC
Comments