forked from alexellis/leaderboard-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema-1.0.sql
69 lines (60 loc) · 2.54 KB
/
schema-1.0.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
drop table activity cascade;
drop table users;
CREATE TABLE users (
user_id integer PRIMARY KEY NOT NULL,
user_login text NOT NULL,
track BOOLEAN NOT NULL,
created_at timestamp not null
);
insert into users (user_id,user_login,track, created_at) values (653013,'alexellisuk',true,now());
insert into users (user_id,user_login,track, created_at) values (103022,'rgee0',true,now());
CREATE TABLE activity (
id INT GENERATED ALWAYS AS IDENTITY,
user_id integer NOT NULL references users(user_id),
activity_type text NOT NULL,
activity_date timestamp NOT NULL,
owner text NOT NULL,
repo text NOT NULL
);
insert into activity (id,user_id,activity_type,activity_date,owner,repo) values (DEFAULT,653013,'issue_created','2019-02-13 07:44:00','openfaas','org-tester');
insert into activity (id,user_id,activity_type,activity_date,owner,repo) values (DEFAULT,653013,'issue_comment','2019-02-13 07:44:05','openfaas','org-tester');
insert into activity (id,user_id,activity_type,activity_date,owner,repo) values (DEFAULT,653013,'issue_comment','2019-02-12 07:44:05','openfaas','org-tester');
insert into activity (id,user_id,activity_type,activity_date,owner,repo) values (DEFAULT,103022,'issue_comment','2019-02-12 07:44:05','openfaas','org-tester');
select * from activity order by activity_date asc;
select a.user_id, a.activity_type, count(a.id) from activity as a
where a.activity_date <= now()
group by (a.user_id, a.activity_type) ;
drop function get_leaderboard;
CREATE or REPLACE FUNCTION get_leaderboard()
RETURNS TABLE(user_id integer, user_login text, issue_comments bigint, issues_created bigint)
AS
$$
BEGIN
RETURN QUERY select
a.user_id,
u.user_login,
count(ic.activity_type) as issue_comments,
count(cm.activity_type) as issues_created
from activity as a
inner join users u
on a.user_id = u.user_id
left outer join activity as ic
on a.user_id= ic.user_id
and a.activity_type = ic.activity_type
and a.activity_date = ic.activity_date
and a.owner = ic.owner
and a.repo = ic.repo
and ic.activity_type = 'issue_comment'
left outer join activity as cm
on a.user_id= cm.user_id
and a.activity_type = cm.activity_type
and a.activity_date = cm.activity_date
and a.owner = cm.owner
and a.repo = cm.repo
and cm.activity_type = 'issue_created'
where u.track = true
group by a.user_id, u.user_login
order by issue_comments desc, issues_created desc;
END
$$ LANGUAGE 'plpgsql' VOLATILE;
select * from get_leaderboard();