45

I have one table named GUYS(ID, NAME, PHONE), and I need to add a count of how many guys have the same name, and at the same time show all of them. So I can't group them.

Example:

ID NAME  PHONE
1  John  335   
2  Harry 444
3  James 367
4  John  742
5  John  654

The desired output should be:

ID NAME  PHONE COUNT
1  John  335   3
2  Harry 444   1
3  James 367   1
4  John  742   3
5  John  654   3

How could I do that? I only managed to get a lot of guys with different counts.

Thanks

cngzz1
  • 143
  • 2
  • 9
east
  • 451
  • 1
  • 4
  • 3

7 Answers7

38

Update for 8.0+: This answer was written well before MySQL version 8, which introduced window functions with mostly the same syntax as the existing ones in Oracle.

In this new syntax, the solution would be

SELECT
  t.name,
  t.phone,
  COUNT('x') OVER (PARTITION BY t.name) AS namecounter
FROM
  Guys t

The answer below still works on newer versions as well, and in this particular case is just as simple, but depending on the circumstances, these window functions are way easier to use.


Older versions: Since MySQL, until version 8, didn't have analytical functions like Oracle, you'd have to resort to a sub-query.

Don't use GROUP BY, use a sub-select to count the number of guys with the same name:

SELECT
  t.name,
  t.phone,
  (SELECT COUNT('x') FROM Guys ct 
   WHERE ct.name = t.name) as namecounter
FROM
  Guys t

You'd think that running a sub-select for every row would be slow, but if you've got proper indexes, MySQL will optimize this query and you'll see that it runs just fine.

In this example, you should have an index on Guys.name. If you have multiple columns in the where clause of the subquery, the query would probably benefit from a single combined index on all of those columns.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 2
    I expected this query to perform slower than redfilter's answer, but that was not the case in my testing. With a million row database, it completed in less than half the time. I do have 'name' indexed. – Tony Aug 02 '12 at 17:33
  • 1
    Worked well in sqlite, using ```using System.Data.SQLite;``` – Diego Montania Oct 06 '22 at 19:22
36

Use an aggregate Query:

select g.ID, g.Name, g.Phone, count(*) over ( partition by g.name ) as Count
from 
Guys g;
Loofer
  • 6,841
  • 9
  • 61
  • 102
Sowmia Naraynan
  • 377
  • 3
  • 2
19

You can still use a GROUP BY for the count, you just need to JOIN it back to your original table to get all the records, like this:

select g.ID, g.Name, g.Phone, gc.Count
from Guys g
inner join (
    select Name, count(*) as Count
    from Guys
    group by Name
) gc on g.Name = gc.Name
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
0

In Oracle DB you can use

SELECT ID,NAME,PHONE,(Select COUNT(ID)From GUYS GROUP BY Name) FROM GUYS ;

aristotll
  • 8,694
  • 6
  • 33
  • 53
0
DECLARE @tbl table 
(ID int,NAME varchar(20), PHONE int)
insert into @tbl
select 
1  ,'John',  335   
union all
select 
2  ,'Harry', 444
union all
select 
3  ,'James', 367
union all
select 
4  ,'John',  742
union all
select 
5  ,'John',  654

SELECT
 ID
 , Name
 , Phone
 , count(*) over(partition by Name)
FROM @tbl 
ORDER BY ID
Roelant
  • 4,508
  • 1
  • 32
  • 62
Nandish
  • 19
  • 2
-1
select id, name, phone,(select count(name) from users u1 where u1.name=u2.name) count from users u2
double-beep
  • 5,031
  • 17
  • 33
  • 41
redoc
  • 1
  • 3
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep Mar 31 '19 at 13:41
-1

try

select column1, count(1) over ()
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 20 '22 at 20:58