忽略一些数据的平均计算
Average calculation ignoring some data
我想计算这些数据的平均值。我不想将完成少于或等于 3 个订单的平均用户包括在内。
我使用这段代码来计算每个用户的订单总数:
SELECT DISTINCT `User`, COUNT(`User`) FROM `DB` GROUP BY`User`;
我得到结果:User1 = 9,User2 = 3,User3 = 8。
我如何计算此数据的平均值,以便不考虑用户,平均值为 8.5
date | User | Type
-----------------------------------
17-01-2022 00:00:00 | User1 | A
17-01-2022 00:00:00 | User1 | A
17-01-2022 00:00:00 | User1 | A
17-01-2022 00:00:00 | User2 | A
18-01-2022 00:00:00 | User3 | B
18-01-2022 00:00:00 | User3 | B
18-01-2022 00:00:00 | User3 | B
19-01-2022 00:00:00 | User1 | B
19-01-2022 00:00:00 | User2 | B
19-01-2022 00:00:00 | User2 | C
19-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User1 | C
21-01-2022 00:00:00 | User1 | A
21-01-2022 00:00:00 | User1 | A
21-01-2022 00:00:00 | User3 | A
21-01-2022 00:00:00 | User1 | A
21-01-2022 00:00:00 | User1 | A
使用HAVING
子句排除订单少于或等于3个的用户,然后计算平均值:
SELECT AVG(counter) AS avg_counter
FROM (
SELECT `User`, COUNT(`User`) AS counter
FROM `DB`
GROUP BY`User`
HAVING counter > 3
) t;
我想计算这些数据的平均值。我不想将完成少于或等于 3 个订单的平均用户包括在内。
我使用这段代码来计算每个用户的订单总数:
SELECT DISTINCT `User`, COUNT(`User`) FROM `DB` GROUP BY`User`;
我得到结果:User1 = 9,User2 = 3,User3 = 8。 我如何计算此数据的平均值,以便不考虑用户,平均值为 8.5
date | User | Type
-----------------------------------
17-01-2022 00:00:00 | User1 | A
17-01-2022 00:00:00 | User1 | A
17-01-2022 00:00:00 | User1 | A
17-01-2022 00:00:00 | User2 | A
18-01-2022 00:00:00 | User3 | B
18-01-2022 00:00:00 | User3 | B
18-01-2022 00:00:00 | User3 | B
19-01-2022 00:00:00 | User1 | B
19-01-2022 00:00:00 | User2 | B
19-01-2022 00:00:00 | User2 | C
19-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User3 | C
20-01-2022 00:00:00 | User1 | C
21-01-2022 00:00:00 | User1 | A
21-01-2022 00:00:00 | User1 | A
21-01-2022 00:00:00 | User3 | A
21-01-2022 00:00:00 | User1 | A
21-01-2022 00:00:00 | User1 | A
使用HAVING
子句排除订单少于或等于3个的用户,然后计算平均值:
SELECT AVG(counter) AS avg_counter
FROM (
SELECT `User`, COUNT(`User`) AS counter
FROM `DB`
GROUP BY`User`
HAVING counter > 3
) t;