0

Im trying to count occurrences of name, but i want each row returned no matter if that name has already been counted. The data looks like;

ID |  NAME  
1     Peter 
2     Simone 
3     Otto
4     Cedric 
5     Peter 
6     Cedric
7     Cedric

The following only returns one row per unique name

select id, first_name, count(first_name)from table group by first_name

ID |  FIRST_NAME  | count(first_name)
1     Peter          2
2     Simone         1
3     Otto           1
4     Cedric         3

But im trying to return every row, something like

ID |  FIRST_NAME  | count(first_name)
1     Peter          2
2     Simone         1
3     Otto           1
4     Cedric         3
5     Peter          2
6     Cedric         3
7     Cedric         3

3 Answers 3

2

If you are using MySQL version >= 8.0, then you can use window functions:

select id,
       first_name,
       count(*) over (partition by first_name)
from table

For earlier versions:

select id,
       first_name,
       (select count(*) from table where first_name = t.first_name)
from table t
Sign up to request clarification or add additional context in comments.

5 Comments

Id seems rather meaningless in this context
@Strawberry OP included it in resultset, so who I am to change those requirements :)
i included "id" simply as an example as it was easier rather than real column name/data. so thanks for including in answer @MichalTurczyn, worked exactly as needed.
@olym If my answer has helped you,you should accept it (green check mark on the left) and optionally upvote.
@MichałTurczyn im unable to upvote or accept answer due to being a newbie. Your solution is the easiest for my situation, however as fancyPants noted below, a correlated subquery might take a performance hit
1

You can use a Correlated subquery:

SELECT t1.id, 
       t1.first_name,
       (SELECT COUNT(id) 
        FROM table t2 
        WHERE t2.first_name = t1.first_name) AS total_count
FROM table t1

Comments

1

Edit: now that I've seen the other answers, why is joining better than using a correlated subquery? Because a correlated subquery is executed for every row in your table. When you join it, the query is executed just once.


Then you have to join those queries.

select * from
table 
inner join (
    select first_name, count(first_name) as name_count from table group by first_name
) qcounts on table.first_name = qcounts.first_name

Also note, that in your query you have to remove id from the select clause, since you neither have it in your group by clause nor do you apply an aggregate function on it. Therefore a random row is returned for this column.
It's a good idea to let MySQL remind you of that by activating the only_full_group_by sql mode. To do this you can do

set global sql_mode = concat(@@global.sql_mode, 'only_full_group_by');

1 Comment

I think you mean table.first_name = qcounts.first_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.