简体   繁体   中英

sql query - how to create 2 levels of count & group

I want to create a count of the number of items for all groups.

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

The query, if supported, may look like this.

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

Any idea how I can do this with a query without using temporary table? Thanks:-)

I think you want two levels of aggregation:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM