def fundamentals_redundant_continuously_query_to_sql(query, trade_day):
    '''
    根据传入的查询对象和起始时间生成sql
    trade_day是要查询的交易日列表
    '''

    from .fundamentals_tables_gen import (
        BalanceSheet, IncomeStatement, CashFlowStatement, FinancialIndicator,
        BankIndicatorAcc, SecurityIndicatorAcc, InsuranceIndicatorAcc, StockValuation
    )

    if query.limit_value:
        limit = min(FUNDAMENTAL_RESULT_LIMIT, query.limit_value)
    else:
        limit = FUNDAMENTAL_RESULT_LIMIT
    offset = query.offset_value
    query = query.limit(None).offset(None)

    def get_table_class(tablename):
        for t in (BalanceSheet, CashFlowStatement, FinancialIndicator,
                  IncomeStatement, StockValuation, BankIndicatorAcc, SecurityIndicatorAcc,
                  InsuranceIndicatorAcc):
            if t.__tablename__ == tablename:
                return t

    def get_tables_from_sql(sql):
        m = re.findall(
            r'cash_flow_statement_day|balance_sheet_day|financial_indicator_day|'
            r'income_statement_day|stock_valuation|bank_indicator_acc|'
            r'security_indicator_acc|insurance_indicator_acc',
            sql
        )
        return list(set(m))
    # 从query对象获取表对象
    tablenames = get_tables_from_sql(str(query.statement))
    tables = [get_table_class(name) for name in tablenames]
    query = query.filter(StockValuation.day.in_(trade_day))
    # 根据stock valuation 表的code和day字段筛选
    for table in tables:
        if table is not StockValuation:
            query = query.filter(StockValuation.code == table.code)
            if hasattr(table, 'day'):
                query = query.filter(StockValuation.day == table.day)
            else:
                query = query.filter(StockValuation.day == table.statDate)

    # 连表
    for table in tables[1:]:
        query = query.filter(table.code == tables[0].code)

    # 恢复 offset, limit
    query = query.limit(limit).offset(offset)
    # query = query.subquery()
    sql = compile_query(query)
    # 默认添加查询code和day作为panel索引
    sql = sql.replace(
        'SELECT ',
        'SELECT DISTINCT stock_valuation.day AS day, stock_valuation.code as code, '
    )
    return sql
根据传入的查询对象和起始时间生成sql
trade_day是要查询的交易日列表
jqdatasdk/finance_service.py