Search Logs - How to get a complete list for a given date range?

I think something like this should be more like it:

-- [params]
-- date :start_date
-- date :end_date


WITH results AS (


SELECT LOWER(term) AS term,
       COUNT(*) AS searches,
       SUM(CASE WHEN search_result_id IS NOT NULL THEN 1 ELSE 0 END) AS click_through
FROM search_logs
WHERE created_at BETWEEN :start_date AND :end_date
GROUP BY term

)


SELECT term AS "Search Term",
       searches AS "Searches",
       click_through AS "Clicks-Through",
       CEIL ((click_through/searches::float) * 100) || '%' AS "CTR"
FROM results
ORDER BY searches DESC

Hopefully that helps, but let me know if you want any tweaks. :slight_smile:

5 Likes