def alltime_features_query(table_name='%(table_name)s'):
    """
    Create alltime features which are part of the final feature set.
    All features are either independent of total magnitude or scaled.

    X_ratio: This feature category calculates the ratio of outgoing OVER
             incoming events over the whole time period.
    scaled_X: This feature category scales event or volume to be bound between
              0 and 1 relative to all other antennas.

    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 whole time period.
    """
    query = """
        SELECT
          antenna_id,
          og_calls/ic_calls as calls_ratio,
          og_sms/ic_sms as sms_ratio,
          og_vol/ic_vol as vol_ratio,
          (og_sms + ic_sms)/(og_calls + ic_calls) as sms2calls_ratio,
          (og_calls - MIN(og_calls) OVER())/(MAX(og_calls) OVER() -
           MIN(og_calls) OVER()) as scaled_og_calls,
          (og_sms - MIN(og_sms) OVER())/(MAX(og_sms) OVER() -
           MIN(og_sms) OVER()) as scaled_og_sms,
          (og_vol - MIN(og_vol) OVER())/(MAX(og_vol) OVER() -
           MIN(og_vol) OVER()) as scaled_og_vol,
          (ic_calls - MIN(ic_calls) OVER())/(MAX(ic_calls) OVER() -
           MIN(ic_calls) OVER()) as scaled_ic_calls,
          (ic_sms - MIN(ic_sms) OVER())/(MAX(ic_sms) OVER() -
           MIN(ic_sms) OVER()) as scaled_ic_sms,
          (ic_vol - MIN(ic_vol) OVER())/(MAX(ic_vol) OVER() -
           MIN(ic_vol) OVER()) as scaled_ic_vol
        FROM
        (
          SELECT
            antenna_id,
            SUM(og_sms) as og_sms,
            SUM(ic_sms) as ic_sms,
            SUM(og_calls) as og_calls,
            SUM(ic_calls) as ic_calls,
            SUM(og_vol) as og_vol,
            SUM(ic_vol) as ic_vol
          FROM %(table_name)s
          GROUP BY antenna_id
        )
    """
    return query % {'table_name': table_name}
