您如何获得每个特定计数分组的记录百分比
How do you get % of records per specific count grouping
我在名为 users
的 table 中有以下数据
userId | orgId
1 | 1
2 | 1
3 | 2
4 | 2
5 | 2
我在名为 activities
的 table 中有以下数据
userId | activityId | type
1 | 11 | a
1 | 12 | a
1 | 13 | a
1 | 14 | b
1 | 15 | b
1 | 16 | c
1 | 17 | c
1 | 18 | c
2 | 19 | a
2 | 20 | a
2 | 21 | c
2 | 22 | c
我需要能够计算每个 orgId
用户中的百分比:
- 在
activities' table where
类型中只有 1 条记录` = 'c'
- 在
activities
table 中有 5 条记录,其中 type
= 'c'
- 在
activities
table 中有 10 条记录,其中 type
= 'c'
- 他们 50% 的记录在
activities
table 其中 type
= 'c'
- 他们 80% 的记录在
activities
table 其中 type
= 'c'
所以结果应该是这种(希望是可能的)格式:
orgId|% who have 1| % who have 5| % who have 10| % who have 50%| % who have 80%
1 | 20 | 54 | 12 | 23 | 32
2 | 22 | 44 | 32 | 44 | 2
3 | 28 | 34 | 24 | 11 | 43
4 | 10 | 14 | 27 | 25 | 45
感谢您的帮助。
您可以使用 "conditional aggregation" 获取每个用户的计数和百分比:
select userId,
sum(type = 'c') as countC,
sum(type = 'c') * 100 / count(*) as percentC
from activities
group by userId
您的示例数据的结果将是:
| userId | countC | percentC |
|--------|--------|----------|
| 1 | 3 | 37.5 |
| 2 | 2 | 50 |
这可以用作与 users
table 连接的子查询。然后你可以再次使用 "conditional aggregation" 得到预期的结果:
select u.orgId,
sum(countC = 1) as `only 1`,
sum(countC >= 5) as `at least 5`,
sum(countC >= 10) as `at least 10`,
sum(percentC >= 50) as `at least 50%`,
sum(percentC >= 80) as `at least 80%`
from (
select userId,
sum(type = 'c') as countC,
sum(type = 'c') * 100 / count(*) as percentC
from activities
group by userId
) a
join users u using(userId)
group by u.orgId
您的样本数据不是该查询的最佳数据。但是你会得到:
| orgId | only 1 | at least 5 | at least 10 | at least 50% | at least 80% |
|-------|--------|------------|-------------|--------------|--------------|
| 1 | 0 | 0 | 0 | 1 | 0 |
我在名为 users
userId | orgId
1 | 1
2 | 1
3 | 2
4 | 2
5 | 2
我在名为 activities
userId | activityId | type
1 | 11 | a
1 | 12 | a
1 | 13 | a
1 | 14 | b
1 | 15 | b
1 | 16 | c
1 | 17 | c
1 | 18 | c
2 | 19 | a
2 | 20 | a
2 | 21 | c
2 | 22 | c
我需要能够计算每个 orgId
用户中的百分比:
- 在
activities' table where
类型中只有 1 条记录` = 'c' - 在
activities
table 中有 5 条记录,其中type
= 'c' - 在
activities
table 中有 10 条记录,其中type
= 'c' - 他们 50% 的记录在
activities
table 其中type
= 'c' - 他们 80% 的记录在
activities
table 其中type
= 'c'
所以结果应该是这种(希望是可能的)格式:
orgId|% who have 1| % who have 5| % who have 10| % who have 50%| % who have 80%
1 | 20 | 54 | 12 | 23 | 32
2 | 22 | 44 | 32 | 44 | 2
3 | 28 | 34 | 24 | 11 | 43
4 | 10 | 14 | 27 | 25 | 45
感谢您的帮助。
您可以使用 "conditional aggregation" 获取每个用户的计数和百分比:
select userId,
sum(type = 'c') as countC,
sum(type = 'c') * 100 / count(*) as percentC
from activities
group by userId
您的示例数据的结果将是:
| userId | countC | percentC |
|--------|--------|----------|
| 1 | 3 | 37.5 |
| 2 | 2 | 50 |
这可以用作与 users
table 连接的子查询。然后你可以再次使用 "conditional aggregation" 得到预期的结果:
select u.orgId,
sum(countC = 1) as `only 1`,
sum(countC >= 5) as `at least 5`,
sum(countC >= 10) as `at least 10`,
sum(percentC >= 50) as `at least 50%`,
sum(percentC >= 80) as `at least 80%`
from (
select userId,
sum(type = 'c') as countC,
sum(type = 'c') * 100 / count(*) as percentC
from activities
group by userId
) a
join users u using(userId)
group by u.orgId
您的样本数据不是该查询的最佳数据。但是你会得到:
| orgId | only 1 | at least 5 | at least 10 | at least 50% | at least 80% |
|-------|--------|------------|-------------|--------------|--------------|
| 1 | 0 | 0 | 0 | 1 | 0 |