Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL question

Posted on 2008-06-09
5
Medium Priority
?
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 800 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 600 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 600 total points
ID: 21746232
select * from urTable
where SecondItemNumber in (select SecondItemNumber from urTable Group by SecondItemNumber having count(*) > 1 )  
0
 
LVL 143

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 143

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

721 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