按列分组 + COUNT(*),如何获得每个组合的平均计数?
我有以下(简化的)查询:
SELECT ResolvedBy, COUNT(*) AS Count, fiCategory, fiSubCategory, fiSymptom
FROM tContact
WHERE (ResolvedBy IS NOT NULL)
GROUP BY ResolvedBy, fiCategory, fiSubCategory, fiSymptom
ORDER BY Count DESC
现在我需要 fiCategory、fiSubCategory、fiSymptom 的每个组合的平均计数作为列。怎么做呢?
例如:
ResolvedBy Count fiCategory fiSubCategory fiSymptom Average
1 50 1 2 3 40
2 30 1 2 3 40
3 40 1 2 3 40
1 20 2 3 4 30
2 40 2 3 4 30
示例中是 fiCategory、fiSubCategory 和 fiSymptom 的两种组合:1,2,3
和 2,3,4
。因此,计算出两个平均值:
- 50+30+40 / 3 = 40
- 20+40 / 2 = 30。
所以我想对每个组合的计数求和并除以出现的次数。
编辑:该示例是提取所需的查询结果。计数是每个 ResolvedBy
出现的所有该组合的总和。
先感谢您。
I have following (simplified) query:
SELECT ResolvedBy, COUNT(*) AS Count, fiCategory, fiSubCategory, fiSymptom
FROM tContact
WHERE (ResolvedBy IS NOT NULL)
GROUP BY ResolvedBy, fiCategory, fiSubCategory, fiSymptom
ORDER BY Count DESC
Now i need the average count for every combination of fiCategory, fiSubCategory, fiSymptom
as column. How to do that?
For example:
ResolvedBy Count fiCategory fiSubCategory fiSymptom Average
1 50 1 2 3 40
2 30 1 2 3 40
3 40 1 2 3 40
1 20 2 3 4 30
2 40 2 3 4 30
In the example are two combinations of fiCategory,fiSubCategory and fiSymptom: 1,2,3
and 2,3,4
. Hence there are two averages that are calculated:
- 50+30+40 / 3 = 40
- 20+40 / 2 = 30.
So i want to sum the count of every combination and divide through the number of occurences.
Edit: The example is an extraction of the desired result of the query. The count is the sum of all occurences of this combination for every ResolvedBy
.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this: