My current SQL looks like this –
SELECT i.id,
i.name,
i.description,
c.name campaign,
count(h.id) hits
FROM items i
LEFT JOIN campaigns c ON c.id = i.campaignid
LEFT JOIN links l ON l.itemid = i.id
LEFT JOIN hits h ON h.linkid = l.id
GROUP BY i.id
I would like to modify this so a 6th column is returned with the count() of h.id, but only including “hits” from the last 30 days. So assuming that there were 100 “hits” overall and 50 in the last thirty days, the 5th column would be 100 and the 6th would be 50.
Apologies if my description is quite wordy…
You could use the shorter (but not necessarily faster):