Created
August 19, 2017 18:47
-
-
Save marchenko1985/3f4f460d441aa41a0dfa3fa8e8fc443d to your computer and use it in GitHub Desktop.
bigquery recommendations based on user ratings
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
| WITH d AS ( | |
| -- should return (cid - user id, vid - item id, rating) | |
| SELECT | |
| clientId as cid, | |
| REGEXP_EXTRACT(page.pagePath, r'/company\d+/vacancy(\d+)') AS vid, | |
| MAX( | |
| CASE | |
| WHEN page.pagePath LIKE '%apply=thanks%' THEN 5 | |
| WHEN page.pagePath LIKE '%mode=apply%' THEN 2 | |
| ELSE 1 | |
| END | |
| ) AS rating | |
| FROM `majestic-cairn-171208`.bigdata.ga WHERE | |
| _PARTITIONTIME BETWEEN TIMESTAMP_TRUNC(TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 * 24 HOUR),DAY) AND TIMESTAMP_TRUNC(TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 * 24 HOUR),DAY) | |
| AND type = 'pageview' | |
| AND page.pagePath LIKE '/company%/vacancy%' | |
| GROUP BY cid, vid | |
| ), v AS ( | |
| -- optional, just remove join in last query, should return (vid - item id, title) | |
| SELECT DISTINCT | |
| REGEXP_EXTRACT(page.pagePath, r'/company\d+/vacancy(\d+)') AS vid, | |
| REGEXP_REPLACE(REGEXP_EXTRACT(page.title, r'(.+) в .+ - .+ | Rabota.ua'), r'\d+ грн$', '') AS title | |
| FROM `majestic-cairn-171208`.bigdata.ga WHERE | |
| _PARTITIONTIME BETWEEN TIMESTAMP_TRUNC(TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 * 24 HOUR),DAY) AND TIMESTAMP_TRUNC(TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 * 24 HOUR),DAY) | |
| AND type = 'pageview' | |
| AND page.pagePath LIKE '/company%/vacancy%' | |
| AND page.pagePath NOT LIKE '%mode=apply%' | |
| AND page.pagePath NOT LIKE '%apply=thanks%' | |
| ), c AS ( | |
| -- correlation | |
| SELECT a.vid as vid, b.vid as similar_vid, CORR(a.rating, b.rating) as correlation, count(*) as `count` | |
| FROM d AS a | |
| JOIN d AS b | |
| ON a.cid = b.cid | |
| WHERE a.vid <> b.vid | |
| GROUP BY a.vid, b.vid | |
| ) | |
| -- get related items to desc | |
| select c.*, v.title from c | |
| join v on c.similar_vid = v.vid | |
| where c.vid = '6849385' | |
| order by `count` desc | |
| limit 10 |
marchenko1985
commented
Jan 10, 2018
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment