0

I have a database table with tweets in a jsonb field. I have a query to get the tweets ordered by the most retweeted, this is what it looks like:

SELECT * FROM (
   SELECT DISTINCT ON (raw->'retweeted_status'->'id_str')
     raw->'retweeted_status' as status,
     raw->'retweeted_status'->'retweet_count' as cnt
   FROM tweet
   WHERE (raw->'retweeted_status') is not null
   ORDER BY raw->'retweeted_status'->'id_str', cnt DESC
) t
ORDER BY cnt DESC

I'm trying to create this query with sqlalchemy, this is where i got so far:

session.query(Tweet.raw['retweeted_status'], 
         Tweet.raw['retweeted_status']['retweet_count'].label('cnt'))\
        .filter(~Tweet.raw.has_key('retweeted_status'))\
        .distinct(Tweet.raw['retweeted_status']['id_str']).order_by(Tweet.raw['retweeted_status']['id_str'].desc()).subquery()

But how to go from that to order by cnt?

applechief
  • 6,615
  • 12
  • 50
  • 70

1 Answers1

0

It may not produce the exact query you have shown but should point you in the right direction: you can use your label 'cnt' in order_by, like: .order_by('cnt').

Moreover you can use your label as an argument for sqlalchemy.desc function. Summing up:

from sqlalchemy import desc

q = (
    session.query(
        Tweet.raw['retweeted_status'],
        Tweet.raw['retweeted_status']['retweet_count'].label('cnt')
    )
    .filter(~Tweet.raw.has_key('retweeted_status'))
    .distinct(
        Tweet.raw['retweeted_status']['id_str']
    )
    .order_by(desc('cnt'))
).subquery()

Additional hint: you can format your query nicely if you put it in parentheses.

You may want to read answers to a general question on python sqlalchemy label usage too.

krassowski
  • 13,598
  • 4
  • 60
  • 92