D
D
d1sconn3ct2021-02-26 20:42:53
SQL Server
d1sconn3ct, 2021-02-26 20:42:53

How to make an ms SQL query so that it counts the same values ​​in a column and records the number of matches?

For example, there is a table like this:

id.........User
1........Who-ho
2..........Who
3.......Who -ho
4........Anybdy
5.........Who You

need to write a query that will show how many matches the "User" column has, by type:

id....... ..user..........matches
1........Who-ho ........2
2..........Who ... ..........2
3.......Who-ho.........2
4........Anybdy........ ...1
5.........Who.............2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2021-02-27
@d1sconn3ct

SELECT ID, User, (SELECT COUNT(*) FROM a AS b WHERE b.User = a.User) AS Matches FROM a

R
rPman, 2021-02-26
@rPman

select count(...) ... group by ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question