def variance_features_query(table_name='%(table_name)s'):
    """
    Create variance features which are part of the final feature set.
    For all ratio variables (outgoing over incoming of calls, sms and
    call volume in seconds) the variance over all weeks is being
    calculated. This feature tries to capture the volatility of the
    relation between outgoing and incoming traffic over time.

    Inputs
    ------
    table_name : name of the table the query is supposed to run on,
                 defaulting to '%(table_name)s', i.e. no substitution

    Output
    ------
    Partial final features dataframe which holds features
    calculated over the variance over weekly ratios.
    """
    query = """
        SELECT
          antenna_id,
          -- variance over these ratios
          VARIANCE(calls_ratio) as calls_ratio_var,
          VARIANCE(sms_ratio) as sms_ratio_var,
          VARIANCE(vol_ratio) as vol_ratio_var
        FROM
        (
          SELECT
            antenna_id,
            week_number,
            -- ratios per week
            SUM(og_calls)/SUM(ic_calls) as calls_ratio,
            SUM(og_sms)/SUM(ic_sms) as sms_ratio,
            SUM(og_vol)/SUM(ic_vol) as vol_ratio
          FROM %(table_name)s
          GROUP BY antenna_id, week_number
        )
        GROUP BY antenna_id
    """
    return query % {'table_name': table_name}
