-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg.session.sql
125 lines (113 loc) · 3.18 KB
/
tg.session.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
-- статистика по реакциям
;
select m.message, m.id, u.computed_name, count(r.id)
from message m
inner join main.reaction r on m.id = r.message_id
inner join main.user u on u.id = m.user_id
group by m.message, m.id
order by count(r.id) desc
;
select m.message, m.id,r.emoji, count(r.id)
from message m
inner join main.reaction r on m.id = r.message_id
group by m.message, m.id, r.emoji
order by count(r.id) desc
;
-- топ сообщений в час по пользователям
WITH RECURSIVE
cnt(x) AS (
SELECT 0
UNION ALL
SELECT x+1 FROM cnt
LIMIT 24
)
select computed_name, hour,max(cnt), date
from (
SELECT u.computed_name , x as hour, count(m.id) as cnt, date(m.date) as date
FROM cnt
inner join message m on cast(strftime('%H', m.date) as real)=cnt.x
INNER join "user" u on u.id =m.user_id
INNER join channel_members cm on cm.user_id =u.id
group by u.id, x, date(m.date)
ORDER by count(m.id) desc
)
group by computed_name
;
-- просто том сообщений в час
WITH RECURSIVE
cnt(x) AS (
SELECT 0
UNION ALL
SELECT x+1 FROM cnt
LIMIT 24
)
SELECT x, count(m.id), date(m.date)
FROM cnt
inner join message m on cast(strftime('%H', m.date) as real)=cnt.x
group by x, date(m.date)
HAVING count(m.id)>100
ORDER by count(m.id) desc
;
--- сообщений по часам
WITH RECURSIVE
cnt(x) AS (
SELECT 0
UNION ALL
SELECT x+1 FROM cnt
LIMIT 24
),
daily as (
SELECT
strftime('%d.%m.%Y', m.date) as date,
strftime('%H:00', m.date) as time,
count(m.id) as cnt
FROM cnt
inner join message m on cast(strftime('%H', m.date) as real)=cnt.x
group by strftime('%d.%m.%Y', m.date),strftime('%H:00', m.date)
)
select
distinct
time,
avg(cnt) over (partition by time),
max(cnt) over (partition by time),
min(cnt) over (partition by time),
first_value(date) over (partition by time order by cnt desc),
first_value(date) over (partition by time order by cnt)
from daily
;
-- кто пишет по часам
WITH RECURSIVE
cnt(x) AS (
SELECT 0
UNION ALL
SELECT x+1 FROM cnt
LIMIT 24
),
daily as (
SELECT
strftime('%d.%m.%Y', m.date) as date,
strftime('%H:00', m.date) as time,
u.computed_name,
count(m.id) as cnt
FROM cnt
inner join message m on cast(strftime('%H', m.date) as real)=cnt.x
inner join main.user u on u.id = m.user_id
group by strftime('%d.%m.%Y', m.date),strftime('%H:00', m.date), u.computed_name
),
for_avg as (
select distinct computed_name, time, cast((sum(cnt) over (partition by computed_name, time)) as real)/(sum(cnt) over (partition by time)) as percent
from daily
)
select
distinct
d.*,
first_value(fa.computed_name) over (partition by d.time order by fa.percent desc )
from (
select distinct
d.time,
first_value(d.computed_name) over (partition by d.time order by cnt desc ) as max_messages,
first_value(d.computed_name) over (partition by d.time order by cnt ) as min_messages
from daily d
) as d
inner join for_avg fa on fa.time=d.time
;