Last active
May 17, 2024 19:14
-
-
Save PaulieScanlon/df260406af966933fa218ee5753503c6 to your computer and use it in GitHub Desktop.
Query to join reactions with analytics data from paulie.dev
This file contains 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({ | |
city: analytics.city, | |
country: analytics.country, | |
flag: analytics.flag, | |
count: sql`COUNT(${reactions.id})`.as('count'), | |
}) | |
.from(analytics) | |
.innerJoin(reactions, eq(analytics.slug, reactions.slug)) | |
.where(and(eq(reactions.reaction, 'happy'), gte(analytics.date, new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000)))) | |
.groupBy(analytics.city, analytics.country, analytics.flag) | |
.orderBy(desc(sql`count`)) | |
.limit(50); |
This file contains 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
WITH happy_reactions AS ( | |
SELECT analytics.city, analytics.country, analytics.flag, COUNT(reactions.id) AS count | |
FROM analytics | |
INNER JOIN reactions ON analytics.slug = reactions.slug | |
WHERE reactions.reaction = 'happy' AND analytics.date >= CURRENT_DATE - INTERVAL '30' DAY | |
GROUP BY analytics.city, analytics.country, analytics.flag | |
) | |
SELECT city, country, flag, count | |
FROM happy_reactions | |
ORDER BY count DESC | |
LIMIT 50; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment