Skip to content

Instantly share code, notes, and snippets.

@lhayhurst
Created February 9, 2016 14:10
Show Gist options
  • Save lhayhurst/2a305fb7ad224de88f45 to your computer and use it in GitHub Desktop.
Save lhayhurst/2a305fb7ad224de88f45 to your computer and use it in GitHub Desktop.
sqlalchemy query example
filters = [
TourneyList.tourney_id == Tourney.id ,
ArchtypeList.id == TourneyList.archtype_id,
Ship.archtype_id == ArchtypeList.id ,
Ship.ship_pilot_id == ShipPilot.id ,
ShipPilot.pilot_id == Pilot.id
]
sql = session.query(
sqlalchemy.extract('year', Tourney.tourney_date).label("year"),
sqlalchemy.extract('month', Tourney.tourney_date).label("month"),
sqlalchemy.extract('day', Tourney.tourney_date).label("day"),
Tourney.tourney_type,
ArchtypeList.faction,
ShipPilot.ship_type,
Pilot.name.label('pilot'),
func.count(TourneyList.id).label("count")).\
filter( and_(*filters)).\
group_by( sqlalchemy.extract('year', Tourney.tourney_date),
sqlalchemy.extract('month', Tourney.tourney_date),
sqlalchemy.extract('day', Tourney.tourney_date),
ArchtypeList.faction,
ShipPilot.ship_type,
Pilot.name).\
statement.compile(dialect=mysql.dialect())
connection = self.pm.db_connector.get_engine().connect()
time_series_data = connection.execute(sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment