How to get number of duplicate Rows of DISTINCT co

2019-08-08 17:14发布

问题:

How can I make the following Column

------------
MakeDistinct
------------
CAT
CAT
CAT
DOG
DOG
PIN
PIN
PIN
PIN

As Shown Below

-------------   -------
AfterDistinct   Count
-------------   -------
CAT              3
DOG              2
PIN              4

回答1:

Use COUNT() function by grouping MakeDistinct column using GROUP BY clause.

  SELECT MakeDistinct AS AfterDistinct
       , COUNT(MakeDistinct) AS Count
    FROM MyTable
GROUP BY MakeDistinct

Output:

╔═══════════════╦═══════╗
║ AFTERDISTINCT ║ COUNT ║
╠═══════════════╬═══════╣
║ CAT           ║     3 ║
║ DOG           ║     2 ║
║ PIN           ║     4 ║
╚═══════════════╩═══════╝

See this SQLFiddle



回答2:

Please try:

select 
    MakeDistinct AfterDistinct, 
    COUNT(*) [Count] 
from 
    YourTable
group by MakeDistinct


回答3:

SELECT MakeDistinct AS AfterDistinct, COUNT(*) AS [COUNT] FROM tablename 
GROUP BY MakeDistinct