Solved

SQL question

Posted on 2008-06-09
5
206 Views
Last Modified: 2010-03-19
I have a table that has a column called 'SecondItemNumber'

How can I return a result-set that returns all duplicate records where SecondItemNumber = SecondItemNumber

0
Comment
Question by:thenrich
  • 3
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 21746222
here we go:
select t.*
  from yourtable t
 where t.SecondItemnumber in ( select i.secondItemnumber from yourtable i group by i.secondItemnumber  having count(*) > 1 ) 

Open in new window

0
 
LVL 8

Assisted Solution

by:sbagireddi
sbagireddi earned 150 total points
ID: 21746231
SELECT SecondItemNumber,
 COUNT(SecondItemNumber) AS NumOccurrences
FROM Table
GROUP BY SecondItemNumber
HAVING ( COUNT(SecondItemNumber) > 1 )
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 150 total points
ID: 21746232
select * from urTable
where SecondItemNumber in (select SecondItemNumber from urTable Group by SecondItemNumber having count(*) > 1 )  
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21746239
if you have a "primary key" or "unique key" field (which will not be secondItemnumber obviously), here a alternative:
select t.*
  from yourtable t
 where exists ( select null from yourtable i where i.secondItemnumber = t.secondItemnumber  and i.that_primary_key <> t.that_primary_key )

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21746240
note: in any case, a index on the field secondItemnumber will be required for a good performance...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question