Created
April 24, 2014 15:41
-
-
Save ryanaslett/11259273 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Burly sql query I wrote to get aggregated statistics from the woefully awful data model that is the ad module in drupal 6.