2016-12-27 11 views
1

モデルsql_query:Leagueは多くのSeasonを持っているがRound多くは、多くのGameTeamを持つ2つの1対1の関係を持っています。SQLAlchemyの:パンダで集計クエリは

ゲームごとの合計ゴールは、SQLAlchemy column_propertyに保存されます。

パンダread_sqlに適切なクエリを渡す方法がわかりません。私がしようとしているすべてのバリエーションは、この含めて、機能していない:

pandoc = pd.read_sql(Match.query.join(Round). 
       join(Season). 
       join(League).filter(Match.round).filter(Round.season).filter(Season.league) 
       .statement, db.session.bind) 
次出力

:(私はいくつかのラウンドを落としました)

total_goals round_id home_goals away_goals finished 
0    1.0 sxxx-0   1.0   0.0  True 
1    0.0 sxxx-0   0.0   0.0  True 
2    2.0 sxxx-0   2.0   0.0  True 
3    3.0 sxxx-0   3.0   0.0  True 

私は理想的には欲しい:

League total_goals 
league.name total_goals (across all seasons) 

Leagueから下をたどってみると、より論理的ですが、それもうまくいきません。

答えて

1

これは働いていたが、私はそれが「ベスト」な方法だかはわからない:

pandoc = pd.read_sql(League.query. 
        join(Season). 
        join(Round). 
        join(Match).with_entities(func.sum(Match.total_goals).label('total_goals'), League.name). 
        group_by(League.name). 
        statement, db.session.bind) 
+0

あなたが生成されたSQLを投稿することができますか? – MaxU