def METHOD_NAME(self):
    if self.statistic in {"count", "max", "min"}:
        sql = f"""
        SELECT subscriber, {self.statistic}({self.characteristic}) AS value
        FROM ({self.handsets_query.get_query()}) AS U
        GROUP BY subscriber
        """
        return sql
    weighted_sum = f"SUM(weight * value)"
    weighted_avg = f"{weighted_sum} / SUM(weight)"
    weighted_var = f"(SUM(weight * (value ^ 2)) - ((({weighted_sum}) ^ 2) / SUM(weight))) / (SUM(weight) - 1)"
    weighted_stddev = f"SQRT({weighted_var})"
    statistic_clause = ""
    if self.statistic in {"sum"}:
        statistic_clause = weighted_sum
    elif self.statistic in {"avg"}:
        statistic_clause = weighted_avg
    elif self.statistic in {"stddev"}:
        statistic_clause = weighted_stddev
    elif self.statistic in {"variance"}:
        statistic_clause = weighted_var
    # Weights are the number of seconds between events, which are
    # calculated using window functions.  The lag function will return NULL
    # for the first event of each series.  In that case we assume that the
    # handset used prior to the first event is the same as the one used in
    # the first event. The generated weights are the number of seconds
    # between this event and the beginning of the observed period. To
    # account for the period between the last event and the end of the
    # observed period we generate an additional row in which we assume the
    # handset used is the same as the one used in the last event. The
    # weights are the number of seconds between this event and the end of
    # the observed period.
    weight_extraction_query = f"""
    WITH W AS (
        SELECT
            subscriber,
            {self.characteristic} AS value,
            time,
            EXTRACT(EPOCH FROM
                time -
                LAG(
                    time,
                    1,
                    TIMESTAMPTZ '{self.handsets_query.start}'
                ) OVER msisdn_by_datetime
            ) AS weight,
            CUME_DIST() OVER msisdn_by_datetime
        FROM ({self.handsets_query.get_query()}) AS U
        WINDOW msisdn_by_datetime AS (PARTITION BY subscriber ORDER BY time)
    )
    SELECT subscriber, value, weight
    FROM W
    UNION ALL
    SELECT subscriber, value, EXTRACT(EPOCH FROM (TIMESTAMPTZ '{self.handsets_query.stop}' - time)) AS weight
    FROM W
    WHERE cume_dist = 1
    """
    # Weighted sum, mean, standard deviation and variance
    if self.statistic in {"sum", "avg", "stddev", "variance"}:
        sql = f"""
        SELECT subscriber, {statistic_clause} AS value
        FROM ({weight_extraction_query}) U
        GROUP BY subscriber
        """
        return sql
    # Weighted median
    if self.statistic == Statistic.MEDIAN:
        return f"""
        WITH W AS ({weight_extraction_query})
        SELECT DISTINCT ON (subscriber) A.subscriber, A.value
        FROM (
            SELECT
                subscriber,
                value,
                weight,
                SUM(weight) OVER (PARTITION BY subscriber ORDER BY weight) AS cum_sum
            FROM W
        ) A
        JOIN ( SELECT subscriber, SUM(weight) AS total_weight FROM W GROUP BY subscriber) B
        ON A.subscriber = B.subscriber AND A.cum_sum >= (B.total_weight / 2)
        ORDER BY A.subscriber, A.weight
        """
    raise NotImplementedError(
        f"{self.statistic} is not implemented for HandsetStats"
    )