forked from ldbc/ldbc_snb_interactive_v1_impls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query15.sql
37 lines (37 loc) · 1.04 KB
/
query15.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
/* Q15. Social normals
\set country '\'Belarus\''
*/
WITH persons_of_country AS (
SELECT p.p_personid AS personid
FROM person p
, place ci -- city
, place co -- country
WHERE 1=1
-- join
AND p.p_placeid = ci.pl_placeid
AND ci.pl_containerplaceid = co.pl_placeid
-- filter
AND co.pl_name = :country
)
, persons_w_friendcount AS (
SELECT p.personid
, count(f.personid) AS friendCount
, floor(avg(count(f.personid)) OVER ()) AS avgFriendCount
FROM persons_of_country p -- persons to find
LEFT JOIN (knows k INNER JOIN persons_of_country f -- friend
ON (k.k_person2id = f.personid))
ON (p.personid = k.k_person1id)
WHERE 1=1
-- join given in the FROM clause
-- filter
GROUP BY p.personid
)
SELECT personid AS "person.id"
, friendCount AS count
FROM persons_w_friendcount
WHERE 1=1
-- filter
AND friendCount = avgFriendCount
ORDER BY personid
LIMIT 100
;