mysql - sql 查询 - 如何创建 2 个级别的计数和组
问题描述
我想为所有组创建项目数的计数。
data
group_id, item_id
1, 1
1, 2
1, 3
2, 1
2, 2
3, 1
3, 2
4, 1
result
set, number_of_items
1, 3
2, 2
1, 1
如果支持,查询可能如下所示。
select count(cs_id) as count, count(count(cs_id) as count) as count2
from max_ecardsent_group_list
group by cs_group_id, count
知道如何在不使用临时表的情况下通过查询来做到这一点吗?谢谢:-)
解决方案
我认为您需要两个级别的聚合:
select num_items, count(*) as num_groups
from (select group_id, count(*) as num_items
from groups
group by group_id
) cnt
group by num_items;
推荐阅读
- python-3.x - 将具有时区的字符串转换为UTC日期时间对象python
- java - Spring Data Rest 中的 NamedEntityGraph 忽略
- html - 如何在右侧制作带有标题的引导轮播?
- jquery - 无法在 jquery 中打印从 Modal 中选择的值
- angular - @viewchild for 指令总是返回元素 ref 而不是指令参考 - angular5
- python - 检索请求对象的源代码
- angular5 - (更改)不适用于角度 5 中的 select2
- php - 我正在尝试使用 laravel 在我的数据库上使用多个过滤器
- blockchain - 如何在 Solidity 中部署后持久化数据
- qt - 在 QML 中有一个凸 MapPolygon