473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Finding duplicates rows in a table

I have to concatenate 2 colimns in a table and find duplicates in them.
I already used the method
select * from tableA a
where (select count(*) from TableA b
where acol1+ +col2 = b.col1+ +col2)>1
But the performance is very bad.
Data is also huge
Can you help me
Thanks in advance
Nov 5 '08 #1
17 2974
ck9663
2,878 Expert 2GB
I have to concatenate 2 colimns in a table and find duplicates in them.
I already used the method
select * from tableA a
where (select count(*) from TableA b
where acol1+ +col2 = b.col1+ +col2)>1
But the performance is very bad.
Data is also huge
Can you help me
Thanks in advance
try this to show the duplicates.

Expand|Select|Wrap|Line Numbers
  1. select a.*
  2. from tableA a
  3. inner join (select col1, col2 from tableA b group by col1, col2 having count(*) > 1) s on a.col1 = s.col1 and s.col2 = a.col2
  4.  

Happy coding!

-- CK
Nov 5 '08 #2
Hi ck9663,
Thank u for u r immediate reply.
But when I run the below query

select Col1 + + Col2
from TableA
group by Col1 + + Col2
having(count(*))>1

I should get the duplicates .
But the the duplicate which I got from the query which u suggested is
different from the above qurey.
Is the above query wrong?
can u please help me
Thanks in advance
Nov 5 '08 #3
ck9663
2,878 Expert 2GB
try:

Expand|Select|Wrap|Line Numbers
  1. select Col1, Col2
  2. from TableA
  3. group by Col, Col2
  4. having count(*) >1
By the way, use the tags when posting.

-- CK
Nov 5 '08 #4
Hi CK
Thank u for u r reply.
The 2 query's which u have suggested r working well.
But The count which both of them return should be the same.
But I am getting 1000 more records with the first one.

Next time when i post a query I will use tags

Can u please help me
If I am not clear with the question me please let me know
Thank u in advance
Nov 5 '08 #5
ck9663
2,878 Expert 2GB
This query will give the duplicate:

Expand|Select|Wrap|Line Numbers
  1. select Col1, Col2, count(*) as cnt
  2. from TableA
  3. group by Col, Col2
  4. having count(*) >1
  5.  
Look at cnt column. If it says "2" it means you have two records on your table with the same col1 and col2. This means it will return the unique combination of the two columns and the number of rows that are duplicate. Not the actual duplicate rows. Make sense?

-- CK
Nov 5 '08 #6
YUP ,GOT U

Thank u So much.
Nov 5 '08 #7
Sorry for troubling you again.
I am loading the data using SSIS.
If there r 2 duplicates only one should be loaded into the
target table ,the remaining should go into another table.
Is this possible in SSIS.

Thank you for helping me and giving me all the above query's
Thank you
Nov 6 '08 #8
ck9663
2,878 Expert 2GB
Am not sure if it's possible in SSIS.

What you could do is to create a trigger on your target table. If the value you're trying to insert on your table is already existing, insert it to another table and rollback the transaction.

On T-SQL, it would've been different. Depending on your case, a DISTINCT might be sufficient enough. If not, you might need to further analyze your table and establish an algorithm on which record to keep and which record to throw.

-- CK
Nov 6 '08 #9
Thank you for u r immedaite answer.
I will try creating trigger.

In SSMS,when we right click on the table
we will get options as script table as create to ...ect
But for me alter to is disabled,what should I do to enable that.
why will that be disabled when I have permissions to devlop in that box
and even execute is disabled.

Can u please help me

Thank you in advance
Nov 6 '08 #10
ck9663
2,878 Expert 2GB
Permission to see the table and do a SELECT on it, yes. Other rights and permissions, no. Call your admin to raise your permission rights.

-- CK
Nov 6 '08 #11
Hi Ck,
Sorry for asking u the same question again.
the query above which u suggested will return all the duplicates in a table
But I want a select statement that will return only one duplicate.
Can u please help me
Thanks in advance
Nov 12 '08 #12
ck9663
2,878 Expert 2GB
Which query?

This:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select Col1, Col2
  3. from TableA
  4. group by Col, Col2
  5. having count(*) >1
  6.  
  7.  
will only return one row for each pair.

-- CK
Nov 12 '08 #13
Thank you for u r immediate reply.

But i need to select all the fileds in the table.
For that I got the query like

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE [dbo].[Employee] (
  2.             [id] [int] NULL ,
  3.             [name] [Varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  4.             [salary] [Numeric](18, 2) NULL 
  5.  ) ON [PRIMARY]
  6.  
  7. Insert into employee values (1,'Ram', 1000.00)
  8. Insert into employee values (1,'Ram', 1000.00)
  9. Insert into employee values (2,'Joe', 2000.00)
  10. Insert into employee values (2,'Joe', 1000.00)
  11. Insert into employee values (3,'Mary', 1000.00)
  12. Insert into employee values (4,'Julie', 5000.00)
  13. Insert into employee values (2,'Joe', 1000.00)
  14. Insert into employee values (1,'Ram', 1000.00)
  15.  
  16. --The above is the table and I am trying to find out duplicates
  17.  
  18. --My query is
  19. SELECT *  FROM employee
  20. WHERE ID IN  (SELECT ID  FROM employee   
  21. WHERE EXIST(SELECT NULL FROM employee AS tmptable     
  22.  WHERE employee.id = tmptable.id
  23.  group by id          
  24. HAVING (employee.id > MIN(tmptable.id))))
But it throwing me errors
Sorry for troubling u again

Thanks in advance
Nov 12 '08 #14
ck9663
2,878 Expert 2GB
Joe (ID = 2) has three records with 2 salary values (1000.00 and 2000.00). Which one do you want to return? Is there a pattern?

-- CK
Nov 13 '08 #15
Hello CK
thank you for u r reply.

I am looking for duplicate Id
Is that possible?

What I thought is, same person cannot have 2 different salaries
so I wanted to check for dupliate id.
Can u please help me.

Thank you
Nov 13 '08 #16
ck9663
2,878 Expert 2GB
To see the complete records of duplicate in employee table, try this:

Expand|Select|Wrap|Line Numbers
  1. select * from employee e1
  2. inner join (select e2.id, count(*) cnt from employee e2 group by id having count(*) > 1) dups on dups.id = e1.id
  3. order by id
  4.  
Happy coding!


-- CK
Nov 13 '08 #17
do u want to completely remove the whole records or leave one and remove the other record.................
Nov 14 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tom Cunningham | last post by:
OK, say you're trying to find all the non-exact duplicates in a table, and especially do it with a single query, so that the check can be part of a user interface. Then naturally you're going...
4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
4
by: Drew | last post by:
I have a permission tracking app that I am working on, and I have made the insert page for it. I am having issues on how to prevent duplicates from getting entered. Currently the interface for...
6
by: Maxi | last post by:
I have 100 tabes in an Access database, every table has 1 filed with 100 names (records), no primary key assigned. I would like to find duplicates. Here is the criteria: The computer should...
3
by: AK | last post by:
Hi Our product uses MS-SQL Server 2000. One of our customer has 10 installations with each installation stroring data in its own database. Now the customer wants to consolidate these databases...
8
by: Mark | last post by:
Let's say I have a table of users, and each user has a list of categories. I could store each user's categories as TEXT with delimeters like "cat1|cat2|cat3" But then I need to be able to get a...
4
by: yin_n_yang74 | last post by:
I am new to SQL and SQL Server world. There must be a simple solution to this, but I'm not seeing it. I am trying to create a crystal report (v8.5) using a stored procedure from SQL Server...
2
by: Radu | last post by:
Hi. I have a "union" table which results of a union of two tables. Occasionally I could have duplicates, when the same PIN has been added to both tables, albeit at different Datees/Times, such...
1
by: tskmjk55 | last post by:
Recently, I have a requirement to develop a vb.net application wherein the input excel sheet data which has an average of 5000 records should be checked for Internal duplicates (duplicates within the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.