vendredi 18 décembre 2015

python sqlalchemy combine query from two tables

I have two tables that are not related like this:

hurstDB = Table('Hurst', metadata,
    Column('symbol_id' , Integer, primary_key=True), 
    Column('Date'      , String(20), nullable=False),
    Column('symbol'    , String(40), nullable=False),
    Column('HurstExp'  , Float,      nullable=False),
)

fundamentalDB = Table('Fundamental', metadata,
    Column('symbol_id' , Integer,    primary_key=True), 
    Column('Date'      , String(20), nullable=False),
    Column('symbol'    , String(40), nullable=False),
    Column('MarketCap' , Float,      nullable=False),
)

Each of the following queries works fine. How do I combine them so I can get only the hurst of companies that are worth more than 50,000,000,000?

# -*- coding: utf-8 -*-
"""
Created on Sun Dec 13 19:22:35 2015

@author: idf
"""

from sqlalchemy import *

def run(stmt):
    rs = stmt.execute()
    for row in rs:
        print(row)

# Let's re-use the same database as before
dbh = create_engine('sqlite:///hurst.db')
dbf = create_engine('sqlite:///fundamental.db')

dbh.echo = True  # We want to see the SQL we're creating
dbf.echo = True  # We want to see the SQL we're creating

metadatah = MetaData(dbh)
metadataf = MetaData(dbf)

# The users table already exists, so no need to redefine it. Just
# load it from the database using the "autoload" feature.
hurst = Table('Hurst',       metadatah, autoload=True)
funda = Table('Fundamental', metadataf, autoload=True)

hurstQ = hurst.select(hurst.c.HurstExp < .5)
run(hurstQ)

fundaQ = funda.select(funda.c.MarketCap > 50000000000)
run(fundaQ)

Aucun commentaire:

Enregistrer un commentaire