Home > Software design >  How to count the number of rows being grouped in group by?
How to count the number of rows being grouped in group by?

Time:05-31

Let's say I have a table with columns as email, topic_id. One email can corresponds to one topic_id multiple times. I want to apply group by based on email and topic_id. But I'm unble to count how many rows are grouped together. Example.

email           topic_id     
[email protected]     1
[email protected]     1 
[email protected]     1
[email protected]     2
[email protected]     1
[email protected]     1
[email protected]     1

The output should give :

    email        topic_id    count   
[email protected]     1            3
[email protected]     2            1
[email protected]     1            3

CodePudding user response:

simple group by and count? :

select email,topic_id, count(*) 
from tablename
group by email,topic_id
  • Related