views:

347

answers:

6

I have a table programparticipants, I am currently successfully querying the IDs where count(name) > 1. What I need now is to query the names that belong to those IDs where count(name) > 1. Can anyone help me please?

Example, data result currently being returned:

        ID     count(name)
        1      2
        3      4
        4      3

Example, data result needed:

        ID     name
        1      nm1
        1      nm3
        3      nm2
        3      nm3
        3      nm4
        3      nm7
        4      nm5
        4      nm8
        4      nm9
+1  A: 
select id, Name 
from programparticipants
where id in ( <your current query selecting only id here> )
Joel Coehoorn
'<your current query selecting only ID and not COUNT(*) here>'?
Jonathan Leffler
Um... yeah. Fixing...
Joel Coehoorn
+1  A: 

You may use this:

SELECT 
   (SELECT name FROM participants WHERE id=p.participantid) AS name
FROM
   programparticipants AS p
WHERE
   .... (the part where you find count(name)>1)
Cem Kalyoncu
This was solved by using everyones suggestions and piecing it together
mattgcon
+3  A: 
   select count(id), name 
    from  programparticipants 
group by name 
  having count(id) > 1
kragan
+1  A: 

I think GROUP BY and HAVING are what you want.

duffymo
Yep, some combination of group by and having are definitely what he needs. There's really not enough detail in the question for us to write the entire query.
Russell Steen
I didn't even attempt it. The link was the best I could come up with quickly.
duffymo
A: 

Think you'll want to look at group by with having:

select id, name, count(name) from table group by 2,1 having count(name) = 2;

You can substitute = 2 for > 1 depending on whatever you want (title says = 2, question says > 1)

Omar Qureshi
A: 
SELECT id, Name
FROM   programparticipants
GROUP BY id, Name
HAVING COUNT(*) > 1
Steve Weet