forked from ldbc/ldbc_snb_interactive_v1_impls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bi-8.sql
54 lines (54 loc) · 1.52 KB
/
bi-8.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Q8. Central Person for a Tag
\set tag '\'Che_Guevara\''
\set date '\'2011-07-22T00:00:00.000+00:00\''::timestamp
*/
WITH person_tag_interest AS (
SELECT p.p_personid AS personid
FROM person p
, person_tag pt
, tag t
WHERE 1=1
-- join
AND p.p_personid = pt.pt_personid
AND pt.pt_tagid = t.t_tagid
-- filter
AND t.t_name = :tag
)
, person_message_score AS (
SELECT p.p_personid AS personid
, count(*) AS message_score
FROM message m
, person p
, message_tag pt
, tag t
WHERE 1=1
-- join
AND m.m_creatorid = p.p_personid
AND m.m_messageid = pt.mt_messageid
AND pt.mt_tagid = t.t_tagid
-- filter
AND m.m_creationdate > :date
AND t.t_name = :tag
GROUP BY p.p_personid
)
, person_score AS (
SELECT coalesce(pti.personid, pms.personid) AS personid
, CASE WHEN pti.personid IS NULL then 0 ELSE 100 END -- scored from interest in the given tag
+ coalesce(pms.message_score, 0) AS score
FROM person_tag_interest pti
FULL JOIN person_message_score pms ON (pti.personid = pms.personid)
)
SELECT p.personid AS "person.id"
, p.score AS score
, sum(f.score) AS friendsScore
FROM person_score p
, knows k
, person_score f -- the friend
WHERE 1=1
-- join
AND p.personid = k.k_person1id
AND k.k_person2id = f.personid
GROUP BY p.personid, p.score
ORDER BY p.score + sum(f.score) DESC, p.personid
LIMIT 100
;