def aggregation_query(self):
    month = self.month.replace(day=1)
    current_month_start = month_formatter(self.month)
    next_month_start = month_formatter((self.month + relativedelta(months=1)))
    query_params = {'month': month_formatter(month), 'state_id': self.state_id, 'current_month_start': current_month_start, 'next_month_start': next_month_start}
    return ('\n        INSERT INTO "{tablename}" (\n          state_id, supervisor_id, month, case_id, latest_time_end_processed, days_ration_given_child\n        ) (\n          SELECT DISTINCT ON (child_health_case_id)\n            %(state_id)s AS state_id,\n            LAST_VALUE(supervisor_id) over w AS supervisor_id,\n            %(month)s AS month,\n            child_health_case_id AS case_id,\n            MAX(timeend) over w AS latest_time_end_processed,\n            SUM(days_ration_given_child) over w AS days_ration_given_child\n          FROM "{ucr_tablename}"\n          WHERE state_id = %(state_id)s AND\n                timeend >= %(current_month_start)s AND timeend < %(next_month_start)s AND\n                child_health_case_id IS NOT NULL\n          WINDOW w AS (PARTITION BY supervisor_id, child_health_case_id)\n        )\n        '.format(ucr_tablename=self.ucr_tablename, tablename=self.tablename), query_params)
def aggregation_query(self):
    month = self.month.replace(day=1)
    current_month_start = month_formatter(self.month)
    next_month_start = month_formatter((self.month + relativedelta(months=1)))
    query_params = {'month': month_formatter(month), 'state_id': self.state_id, 'current_month_start': current_month_start, 'next_month_start': next_month_start}
    return ('\n        INSERT INTO "{tablename}" (\n          state_id, supervisor_id, month, case_id, latest_time_end_processed, days_ration_given_child\n        ) (\n          SELECT DISTINCT ON (child_health_case_id)\n            %(state_id)s AS state_id,\n            LAST_VALUE(supervisor_id) over w AS supervisor_id,\n            %(month)s AS month,\n            child_health_case_id AS case_id,\n            MAX(timeend) over w AS latest_time_end_processed,\n            SUM(days_ration_given_child) over w AS days_ration_given_child\n          FROM "{ucr_tablename}"\n          WHERE state_id = %(state_id)s AND\n                timeend >= %(current_month_start)s AND timeend < %(next_month_start)s AND\n                child_health_case_id IS NOT NULL\n          WINDOW w AS (PARTITION BY supervisor_id, child_health_case_id)\n        )\n        '.format(ucr_tablename=self.ucr_tablename, tablename=self.tablename), query_params)