Skip to content

Instantly share code, notes, and snippets.

@iconara
Created August 25, 2009 19:56
Show Gist options
  • Save iconara/174974 to your computer and use it in GitHub Desktop.
Save iconara/174974 to your computer and use it in GitHub Desktop.
SELECT
category,
segment,
SUM(exposures) AS exposures,
SUM(impressions)/(SUM(exposures) - SUM(indeterminate_visibility)) AS impression_rate,
SUM(engagements)/SUM(exposures) AS engagement_rate
FROM (
SELECT
date,
ad_id,
category,
segment,
MAX(exposures) AS exposures,
MAX(impressions) AS impressions,
MAX(engagements) AS engagements,
MAX(indeterminate_visibility) AS indeterminate_visibility
FROM
report_metrics
WHERE
keyword in (#keywords#)
AND date BETWEEN '#start_date#' AND '#end_date#'
AND agency_id = '#account_id#'
GROUP BY
ad_id, date, category, segment
ORDER BY
category, segment, date
) AS metrics_by_day
GROUP BY
metrics_by_day.category, metrics_by_day.segment
ORDER BY
metrics_by_day.category, metrics_by_day.segment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment