简体   繁体   中英

Select values from column with more than one values in another cumulus

I have table X with 2 columns : ID , name

I need to select only the names with more than 1 ID (and count how much ID those name has)

table X

|namme|ID  |
------------
|A    |1   |
------------
|A    |2   |
------------
|B    |1   |
------------
|C    |1   |
------------
|C    |4   |
------------
|C    |7   |
------------

from the table bellow the answer will be like:

|namme|ID Count|
----------------
|A    |2       |   
----------------
|C    |3       |   
----------------

name A has 2 IDs

name C has 3 IDs

Try, like:

   SELECT Name
    ,COUNT(ID)
FROM Xtable
GROUP BY Name
HAVING COUNT(ID) > 1

use aggregate function count()

 select name,count(*) as cnt from table  group by name
 having count(*)>1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM