Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select duplicate records from two columns

Author  Topic 

getur.srikanth@gmail.com
Yak Posting Veteran

77 Posts

Posted - 2010-07-29 : 10:15:07
I have a table having duplicate records. Columns are same except 2 columns. Please below example




How can I write query for this?

thanks in advance for your help.

Devart
Posting Yak Master

102 Posts

Posted - 2010-07-29 : 10:28:20
Hello,

For example:

Query 1:
SELECT
Title,Section,PTD
FROM A _a
WHERE (select count(*) from A where Title=_a.Title and PTD=_a.PTD)>1;

Query 2:
SELECT
Title,Section,PTD
FROM A _a
WHERE (select count(*) from A where Title=_a.Title and Section=_a.Section)>1;

Best regards,

Devart Team
Go to Top of Page

Celko
Starting Member

23 Posts

Posted - 2010-07-29 : 13:53:05
quote:
Originally posted by getur.srikanth@gmail.com

I have a table having duplicate records [sic: rows are not records]. Columns are same except 2 columns. Please below example




How can I write query for this?

thanks in advance for your help.



Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you want to learn how to ask a question on a Newsgroup, look at: http://www.catb.org/~esr/faqs/smart-questions.html


SELECT vague_id, job_title, MIN(section_name), ptd
FROM TableA
GROUP BY vague_id, job_title, ptd
HAVING COUNT(*) > 1;

SELECT vague_id, job_title, section_name, ptd
FROM TableA
GROUP BY vague_id, job_title, section_name, ptd
HAVING COUNT(vague_id) > 1;



Author of
SQL for Smarties
Thinking in Sets
SQL Puzzles and Answers
Analytics and OLAP in SQL
SQL Programming Style
Standards in SQL
Data & Databases
Trees & Hierarchies in SQL
Go to Top of Page
   

- Advertisement -