Home » SQL & PL/SQL » SQL & PL/SQL » Selecting duplicate values
Selecting duplicate values [message #241981] Thu, 31 May 2007 05:08 Go to next message
garyt
Messages: 8
Registered: May 2007
Junior Member
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
Re: Selecting duplicate values [message #241983 is a reply to message #241981] Thu, 31 May 2007 05:17 Go to previous messageGo to next message
Cthulhu
Messages: 381
Registered: September 2006
Location: UK
Senior Member
For this to return anything, but Name and ID (not just ID) would have to be duplicated.

[Updated on: Thu, 31 May 2007 05:18]

Report message to a moderator

Re: Selecting duplicate values [message #241984 is a reply to message #241981] Thu, 31 May 2007 05:20 Go to previous messageGo to next message
saibal
Messages: 111
Registered: March 2007
Location: India
Senior Member
scott@DBASE-SQL>>select * from s order by empno desc;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

scott@DBASE-SQL>>select empno, ename from s where empno in(
  2  select empno from s group by empno having count(*)>1)order  by ename;

     EMPNO ENAME
---------- ----------
      7499 ALLEN
      7499 ALLEN
      7499 ALLEN
      7698 BLAKE
      7698 BLAKE
      7782 CLARK
      7782 CLARK
      7566 JONES
      7566 JONES
      7654 MARTIN
      7654 MARTIN
      7369 SMITH
      7369 SMITH
      7369 SMITH
      7521 WARD
      7521 WARD
      7521 WARD

17 rows selected.
Re: Selecting duplicate values [message #241985 is a reply to message #241981] Thu, 31 May 2007 05:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68718
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
select id, name
from (
select id, name, 
       count(*) over (partition by id) cnt
from AADETAILS
)
where cnt > 1
/

Note: NAME is a reserved word, don't use it.

Regards
Michel
Re: Selecting duplicate values [message #241989 is a reply to message #241981] Thu, 31 May 2007 05:30 Go to previous message
garyt
Messages: 8
Registered: May 2007
Junior Member
Thank you, both those queries have fixed my problem.
Previous Topic: Dynamic SQL In Stored Procedures
Next Topic: commit
Goto Forum:
  


Current Time: Tue Dec 03 11:08:54 CST 2024