如何根据另一个 table 的 GROUP BY 函数计算 table 中的每日平均频率?
How to calculate daily average frequency in a table in function of GROUP BY from another table?
我想知道每天哪个电影院更频繁,我需要这些值作为相对频率。
我的表是:
表 1:
client_id
cinema_room
1
A
2
B
3
C
4
C
5
A
6
C
7
B
8
A
9
B
10
B
11
B
12
A
13
C
14
A
15
A
16
B
Table 2:
day
client_id
01/01/2022
1
01/01/2022
1
01/02/2022
1
01/02/2022
2
01/02/2022
4
01/03/2022
8
01/04/2022
14
01/04/2022
15
01/04/2022
16
所以我需要这样的结果:
day
cinema_room
avg_freq
01/01/2022
A
1
01/01/2022
B
0
01/01/2022
C
0
01/02/2022
A
0.33
01/02/2022
B
0.33
01/02/2022
C
0.33
01/03/2022
A
1
01/03/2022
B
0
01/03/2022
C
0
01/04/2022
A
0.66
01/04/2022
B
0.33
01/04/2022
C
0
我现在得到的是:
SELECT day , cinema_room, COUNT(t2.client_id) as t2_tot
FROM table1 t1
LEFT JOIN table2 t2 ON t1.client_id = t2.client_id
GROUP BY day, cinema_room
ORDER BY day
其不足的结果是:
NOTE 1: I'm counting, not even averaging.
NOTE 2: The first three rows are wrong af.
day
cinema_room
count_freq
None
A
0
None
B
0
None
C
0
01/01/2022
A
3
01/01/2022
B
0
01/01/2022
C
0
01/02/2022
A
1
01/02/2022
B
1
01/02/2022
C
1
01/03/2022
A
1
01/03/2022
B
0
01/03/2022
C
0
01/04/2022
A
2
01/04/2022
B
1
01/04/2022
C
0
您可以尝试在一个子查询中使用 CROSS JOIN
,这将是一个日历 table,然后在另一个子查询中聚合 table1
& table2
每个 get 计数cinema_room
每天。
SELECT t1.day , t1.cinema_room, SUM(IFNULL(t2.cnt,0)) / (SELECT COUNT(*) FROM table2 tt2 WHERE t1.day = tt2.day) as t2_tot
FROM (
SELECT DISTINCT day,cinema_room
FROM table2 t2 CROSS JOIN table1
) t1
LEFT JOIN (
SELECT day,cinema_room,COUNT(*) cnt
FROM table1 t1 INNER JOIN table2 t2
ON t1.client_id = t2.client_id
GROUP BY day,cinema_room
) t2 ON t1.day = t2.day AND t1.cinema_room = t2.cinema_room
GROUP BY t1.day , t1.cinema_room
ORDER BY t1.day
我想知道每天哪个电影院更频繁,我需要这些值作为相对频率。
我的表是:
表 1:
client_id | cinema_room |
---|---|
1 | A |
2 | B |
3 | C |
4 | C |
5 | A |
6 | C |
7 | B |
8 | A |
9 | B |
10 | B |
11 | B |
12 | A |
13 | C |
14 | A |
15 | A |
16 | B |
Table 2:
day | client_id |
---|---|
01/01/2022 | 1 |
01/01/2022 | 1 |
01/02/2022 | 1 |
01/02/2022 | 2 |
01/02/2022 | 4 |
01/03/2022 | 8 |
01/04/2022 | 14 |
01/04/2022 | 15 |
01/04/2022 | 16 |
所以我需要这样的结果:
day | cinema_room | avg_freq |
---|---|---|
01/01/2022 | A | 1 |
01/01/2022 | B | 0 |
01/01/2022 | C | 0 |
01/02/2022 | A | 0.33 |
01/02/2022 | B | 0.33 |
01/02/2022 | C | 0.33 |
01/03/2022 | A | 1 |
01/03/2022 | B | 0 |
01/03/2022 | C | 0 |
01/04/2022 | A | 0.66 |
01/04/2022 | B | 0.33 |
01/04/2022 | C | 0 |
我现在得到的是:
SELECT day , cinema_room, COUNT(t2.client_id) as t2_tot
FROM table1 t1
LEFT JOIN table2 t2 ON t1.client_id = t2.client_id
GROUP BY day, cinema_room
ORDER BY day
其不足的结果是:
NOTE 1: I'm counting, not even averaging.
NOTE 2: The first three rows are wrong af.
day | cinema_room | count_freq |
---|---|---|
None | A | 0 |
None | B | 0 |
None | C | 0 |
01/01/2022 | A | 3 |
01/01/2022 | B | 0 |
01/01/2022 | C | 0 |
01/02/2022 | A | 1 |
01/02/2022 | B | 1 |
01/02/2022 | C | 1 |
01/03/2022 | A | 1 |
01/03/2022 | B | 0 |
01/03/2022 | C | 0 |
01/04/2022 | A | 2 |
01/04/2022 | B | 1 |
01/04/2022 | C | 0 |
您可以尝试在一个子查询中使用 CROSS JOIN
,这将是一个日历 table,然后在另一个子查询中聚合 table1
& table2
每个 get 计数cinema_room
每天。
SELECT t1.day , t1.cinema_room, SUM(IFNULL(t2.cnt,0)) / (SELECT COUNT(*) FROM table2 tt2 WHERE t1.day = tt2.day) as t2_tot
FROM (
SELECT DISTINCT day,cinema_room
FROM table2 t2 CROSS JOIN table1
) t1
LEFT JOIN (
SELECT day,cinema_room,COUNT(*) cnt
FROM table1 t1 INNER JOIN table2 t2
ON t1.client_id = t2.client_id
GROUP BY day,cinema_room
) t2 ON t1.day = t2.day AND t1.cinema_room = t2.cinema_room
GROUP BY t1.day , t1.cinema_room
ORDER BY t1.day