Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer ORM solution to SQL #25

Open
gregziegan opened this issue Mar 15, 2021 · 0 comments
Open

Prefer ORM solution to SQL #25

gregziegan opened this issue Mar 15, 2021 · 0 comments

Comments

@gregziegan
Copy link
Collaborator

It's debatable whether sql or python is more readable, but this query doesn't quite work in python. I'm awaiting a stable release of SQL alchemy 1.4 with Flask-SQLAlchemy

most_warrants = between_dates(start, end,
      db.session.query(
          Attorney.name,
          func.count(DetainerWarrant.docket_id).label('warrantCount')
      ))\
      .select_from(Attorney)\
      .join(Plantiff)\
      .join(DetainerWarrant)\
      .group_by(Attorney.id, Attorney.name)\
      .order_by(desc(func.count(DetainerWarrant.docket_id)))\
      .subquery()

rest_of_warrants = db.session.query(
      most_warrants.c.name.label('ALL OTHER'),
      func.sum(most_warrants.c.warrantCount).label('warrantCount')
  ).filter(most_warrants.c.name.notin_(most_warrants.c.name).limit(5))

return most_warrants.union(rest_of_warrants).order_by(desc(most_warrants.c.warrantCount)).limit(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant