473,836 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query to eliminate dulicate rows

5 New Member
Hi,

Do anyone know a query for the following senario,

I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

For example
Expand|Select|Wrap|Line Numbers
  1. create table Employee
  2. {
  3. empno char(5),
  4. name varchar2(20),
  5. salary number(5,2)
  6. };
  7.  
the content of the table is

emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50

how to delete the duplicate entries in the above table ????
Nov 13 '06 #1
10 5361
minapatel
12 New Member
Hi,

Do anyone know a query for the following senario,

I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

For example

create table Employee
{
empno char(5),
name varchar2(20),
salary number(5,2)
};

the content of the table is

emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50

how to delete the duplicate entries in the above table ????

Try where the row count is more than one delete the extra rows....
Nov 14 '06 #2
VitorLeite
1 New Member
Hi,

Do anyone know a query for the following senario,

I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

For example

create table Employee
{
empno char(5),
name varchar2(20),
salary number(5,2)
};

the content of the table is

emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50

how to delete the duplicate entries in the above table ????

Have you tried using
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT(empno) FROM Employee
  2.  
Oh, about the deletion part, i'm not really sure, but you should save the results of the 1st query, truncate the table, and insert them back in it again.

Hope it helps
Nov 14 '06 #3
rkamuni
3 New Member
Hi, You can use the follwoing query.
Expand|Select|Wrap|Line Numbers
  1. delete from employee e where e.rowid not in ( select min(b.rowid) from employee b where e.primarykeyfld = b.primarykeyfld)
  2.  
Hope this will solve ur issue.

Thanks,

Hi,

Do anyone know a query for the following senario,

I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

For example

create table Employee
{
empno char(5),
name varchar2(20),
salary number(5,2)
};

the content of the table is

emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50

how to delete the duplicate entries in the above table ????
Nov 14 '06 #4
ck1004
3 New Member
Hi,

Do anyone know a query for the following senario,

I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

For example

create table Employee
{
empno char(5),
name varchar2(20),
salary number(5,2)
};

the content of the table is

emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50

how to delete the duplicate entries in the above table ????
Expand|Select|Wrap|Line Numbers
  1. delete from employee where rowid not in (select max(rowid) from employee group by empno); 
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. delete from Employee emp where rowid <(select min(rowid)  from Employee emp1 where emp.empno=emp12.empno);
  2.  
Nov 15 '06 #5
ck1004
3 New Member
[quote=ranjitkum ar]Hi,

Do anyone know a query for the following senario,

I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

For example

create table Employee
{
empno char(5),
name varchar2(20),
salary number(5,2)
};

the content of the table is

emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50

how to delete the duplicate entries in the above table ????[/delete from employee where rowid not in (select max(rowid) from employee group by empno);
or
delete from employee emp where rowid <(select min(rowid) from employee emp1 where emp.empno=emp1. empno); ]
Nov 15 '06 #6
pragatiswain
96 Recognized Expert New Member
I am not really sure what you are really looking for.
Some times I do the following. It is much faster with huge data volume (Millions of records) and safe.
1. Insert distinct records into a backup table.
2. Truncate Original table.
3. Insert records in original table from backuptable.

Hope this helps
Nov 16 '06 #7
suvam
31 New Member
u may try with the either one to delete duplicate rows --->

Expand|Select|Wrap|Line Numbers
  1. 1. Delete from T1 a
  2. where a.rowid !=(Select min(b.rowid) from T1 b where b.pk_key = a.pk_key) ;
  3.  
Expand|Select|Wrap|Line Numbers
  1. 2. delete from T1
  2. group by pk_key
  3. having count(pk_key) > 1 ;
  4.  
Dec 6 '06 #8
akhileshkumar18
1 New Member
Hi,

Do anyone know a query for the following senario,

I have a table in which i have multiple rows with the same entry. I want to keep only one copy of such rows. How to write a query to delete all the duplicate rows and have only one entry for such with a single query???

For example

create table Employee
{
empno char(5),
name varchar2(20),
salary number(5,2)
};

the content of the table is

emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50
emp01 ranjit 10000.50

how to delete the duplicate entries in the above table ????
Hi Ranjit,
You can try this
Expand|Select|Wrap|Line Numbers
  1. delete from Employee
  2. where empno ='emp01'
  3. AND name ='ranjit'
  4. AND salary = 10000.50
  5. AND rownum = 1
  6.  
Run this query one less then the no of records.
In this example there are 6 entries of the same so you have to run this query 5 times

Regards
Akhilesh
Nov 15 '07 #9
amitpatel66
2,367 Recognized Expert Top Contributor
All,

Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Nov 15 '07 #10

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

Similar topics

0
1574
by: Brian Newsham | last post by:
------=_NextPart_000_004F_01C352B1.E5B8FA20 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I'm working on a PHP based website that loads custom pricing for users = where they logon.
3
4236
by: Jason | last post by:
I will explain (or at least try to) first and then give an example after. I need to append a number of rows from several tables into one master table. Unfortunately there are certain columns (which are UNIQUE columns in the master table) in the source tables that are repeated in the same table or across the source tables. Example: Source 1 Source 2
3
7888
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works something like this: For a (very) simple table containing three rows like this: row 1: A
8
3276
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run simultaneously 4 similar queries it takes nearly 5 minutes instead of 4 times 9 seconds or something near of that. here is a sample query: select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon in (select distinct fomeazon...
6
4574
by: yongsing | last post by:
I have a table containing 5.1 million rows. The DDL of the table is shown below (only relevant parts shown). CREATE TABLE XXX.XXXX ( ... SERIAL CHAR(12) NOT NULL, ENDDATE DATE NOT NULL, ENDTIME TIME NOT NULL, ... ...
21
6991
by: CSN | last post by:
I have a pretty simple select query that joins a table (p) with 125K rows with another table (pc) with almost one million rows: select p.* from product_categories pc inner join products p on pc.product_id = p.id where pc.category_id = $category_id order by p.title
7
3395
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
1
1950
by: Ike | last post by:
I have a simple query of joined tables that is failing to give me any rows of data (though, in checking by hand, it certainly should). Essentially, I am trying to return all rows from `ups` that have `floattime` not equal to it's default value of '0' (please not, in this db dates and times are saved as varchars - so essentially I am comparing strings) SELECT ups.date,associates.branch ,associates.username ,ups.time ,ups.floattime...
3
11825
by: db55 | last post by:
How does the phrase "Is Not Null" in the where clause effect the effectiveness of a query? If it is a determent to the effectiveness of the query, how do you work around it? Thanks,
0
9826
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10860
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10604
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4466
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.