Skip to content

Instantly share code, notes, and snippets.

@ryanaslett
Created April 24, 2014 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanaslett/11259273 to your computer and use it in GitHub Desktop.
Save ryanaslett/11259273 to your computer and use it in GitHub Desktop.
SELECT
*,
(DistinctClicks / DistinctViews) AS DCTR,
(TotalClicks / TotalViews) AS TCTR
FROM
(
SELECT
n.nid,
n.title,
FROM_UNIXTIME(a.autoexpire) AS Expires,
FROM_UNIXTIME(a.expired) AS Expired,
Count(*) AS DistinctViews,
(
SELECT COUNT(*)
FROM ad_statistics as2
WHERE as2.aid = as1.aid AND as2.action = 'click'
) AS DistinctClicks,
SUM(as1.count) AS TotalViews,
(
SELECT SUM(as2.count)
FROM ad_statistics as2
WHERE as2.aid = as1.aid AND as2.action = ‘click'
) AS TotalClicks
FROM node n
LEFT JOIN ad_statistics as1 on as1.aid = n.nid and as1.action = 'view'
LEFT JOIN ads a on a.aid = n.nid
WHERE n.type = 'ad' and as1.aid IS NOT NULL
GROUP BY as1.aid
) T1
ORDER BY DCTR DESC
@ryanaslett
Copy link
Author

Burly sql query I wrote to get aggregated statistics from the woefully awful data model that is the ad module in drupal 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment