tommo74 Posted May 30, 2007 Share Posted May 30, 2007 I have a table called AADETAILS with two colums ('Name' and 'ID'). The ID column is not supposed to have any duplicates, so I need a query that returns a list of all the duplicate ID's (with their Name). For example, if there is an ID with a value of 123 duplicated three times and an ID with a value of 456 duplicated four times, I need a list of the Name and ID of the three instances of 123 and the four instances of 456. I have the following : SELECT Name, ID FROM AADETAILS GROUP BY Name, ID HAVING COUNT(ID) > 1 its not returning anything, but it should (I can see the duplicates in the table if I view all the data in it), whats wrong with that query ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/53568-solved-selecting-duplicate-values/ Share on other sites More sharing options...
pikemsu28 Posted May 30, 2007 Share Posted May 30, 2007 drop the group by name and it should work SELECT Name, ID FROM AADETAILS GROUP BY ID HAVING COUNT(ID) > 1 Quote Link to comment https://forums.phpfreaks.com/topic/53568-solved-selecting-duplicate-values/#findComment-264806 Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 That will give the duplicated ids but only one of the names. Try SELECT id , GROUP_CONCAT(name ORDER BY name SEPARATOR ', ') as names FROM aadetails GROUP BY id HAVING COUNT(*) > 1 Quote Link to comment https://forums.phpfreaks.com/topic/53568-solved-selecting-duplicate-values/#findComment-265246 Share on other sites More sharing options...
tommo74 Posted May 31, 2007 Author Share Posted May 31, 2007 Thanks, when I try and run the query it gives the following error: Error starting at line 1 in command: SELECT id , GROUP_CONCAT(name ORDER BY name SEPARATOR ', ') as names FROM aadetails GROUP BY id HAVING COUNT(*) > 1 Error at Command Line:1 Column:30 Error report: SQL Error: ORA-00907: missing right parenthesis I've tried playing around with the parenthesis but nothing doing. Quote Link to comment https://forums.phpfreaks.com/topic/53568-solved-selecting-duplicate-values/#findComment-265391 Share on other sites More sharing options...
bubblegum.anarchy Posted May 31, 2007 Share Posted May 31, 2007 Confirm that Oracle uses the same GROUP_CONCAT function as MySQL does. EDIT: Oracle does not appear have a group_concat function. Quote Link to comment https://forums.phpfreaks.com/topic/53568-solved-selecting-duplicate-values/#findComment-265393 Share on other sites More sharing options...
tommo74 Posted May 31, 2007 Author Share Posted May 31, 2007 Yes, it appears that you are right about that. Sorry I should have mentioned I'm using Oracle. Quote Link to comment https://forums.phpfreaks.com/topic/53568-solved-selecting-duplicate-values/#findComment-265401 Share on other sites More sharing options...
tommo74 Posted May 31, 2007 Author Share Posted May 31, 2007 I ended up using this which works fine: select name, id from aadetails where id in(select id from aadetails group by id having count(*)>1) order by name; Quote Link to comment https://forums.phpfreaks.com/topic/53568-solved-selecting-duplicate-values/#findComment-265490 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.