Skip to content

Instantly share code, notes, and snippets.

@erickguan
Created November 28, 2017 20:07
Show Gist options
  • Save erickguan/6009aa07bbb9fb1b73a6ae09477228bb to your computer and use it in GitHub Desktop.
Save erickguan/6009aa07bbb9fb1b73a6ae09477228bb to your computer and use it in GitHub Desktop.
WAM2017 stat. Loaded to PostgreSQL by pgloader
-- 20 global top users (40 fetched)
SELECT
a.User AS username,
COUNT(a.NAME) AS articleCount,
e.Code AS editathonCode,
e.Description AS ediathonDescription
FROM
fountain.article a
LEFT JOIN fountain.editathon e ON a.EditathonId = e.Id
WHERE (LOWER(e.Code)
LIKE 'asian-month-2017-%')
GROUP BY
a.User,
e.Code,
e.Description
ORDER BY
articleCount DESC
LIMIT 40;
-- 10 top users in each compaign (25 each fetched)
WITH list AS (
SELECT
a.User,
COUNT(1) AS articleCount,
e.Code,
e.description
FROM
fountain.article a
JOIN fountain.editathon e ON e.id = a.editathonid
WHERE (LOWER(e.code)
LIKE 'asian-month-2017-%')
GROUP BY
a.User,
a.editathonid,
e.code,
e.description
ORDER BY
e.code,
articleCount DESC),
sortedQuery AS (
SELECT
ROW_NUMBER()
OVER (PARTITION BY
list.code) AS rowNumber,
list.*
FROM
list
ORDER BY
list.code,
list.articleCount DESC
)
SELECT
*
FROM
sortedQuery
WHERE
rowNumber <= '25'
;
-- TEST: 10 top users
SELECT
a.user,
COUNT(a.NAME) AS articleCount
FROM
fountain.article a
WHERE
a.editathonid = 151
GROUP BY
a.user
ORDER BY
articleCount DESC;
-- articles submitted per DAY
SELECT
COUNT(1),
a.dateadded::DATE AS dayAdded,
e.code,
e.description
FROM
fountain.article a
JOIN fountain.editathon e ON e.id = a.editathonid
WHERE (LOWER(e.code)
LIKE 'asian-month-2017-%')
GROUP BY
dayAdded,
e.code,
e.description
ORDER BY
dayAdded,
e.code;
-- weekly updates BY campaign
SELECT
COUNT(1),
EXTRACT(WEEK FROM a.dateadded) AS weekAdded,
e.code,
e.description
FROM
fountain.article a
JOIN fountain.editathon e ON e.id = a.editathonid
WHERE (LOWER(e.code)
LIKE 'asian-month-2017-%')
GROUP BY
weekAdded,
e.code,
e.description
ORDER BY
weekAdded,
e.code;
-- total BY campaign
SELECT
COUNT(1),
e.code,
e.description
FROM
fountain.article a
JOIN fountain.editathon e ON e.id = a.editathonid
WHERE (LOWER(e.code)
LIKE 'asian-month-2017-%')
GROUP BY
e.code,
e.description
ORDER BY
e.code;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment