How to Delete duplicates rows without using rowid/distinct.
How to delete duplicates rows present in a table without using rowid or even distinct.
How about:
SQL> SELECT * FROM t1;
A B
1 2
2 3
1 2
4 4
4 4
4 4
SQL> DELETE FROM t1
2 WHERE (a, b) IN (SELECT a, b FROM t1
3 GROUP BY a, b
4 HAVING COUNT(*) > 1) and
5 rownum = 1;
1 row deleted.
SQL> /
1 row deleted.
SQL> /
1 row deleted.
SQL> /
0 rows deleted.
SQL> SELECT * FROM t1;
A B
2 3
1 2
4 4Although, if I was asked a similar question with all those restrictions, my first response would be along the lines of: Is this question indicative of the way I will have to work if I join this company? If so, there is no point answering because I wouldn't touch this job with a ten foot pole.
Similar Messages
-
How to delete duplicate row in certain case
Hi all,</p>
<p>I need you help to delete my duplicate row.</p>
<p>Sample Data in table (<b>table1</b>)</p>
<p style="margin-top: 0; margin-bottom: 0"> </p>
<p style="margin-top: 0; margin-bottom: 0"><b>Timein
DateIn
locationin useridin
status</b></p>
<p style="margin-top: 0; margin-bottom: 0">08:20:00 01/09/2007
0001
U01
1</p>
<p style="margin-top: 0; margin-bottom: 0">08:10:30 01/09/2007
0004
U01
1</p>
<p style="margin-top: 0; margin-bottom: 0">07:20:00 01/09/2007
0006
U01
1</p>
<p style="margin-top: 0; margin-bottom: 0">08:14:00 01/09/2007
0001
U02
1</p>
<p style="margin-top: 0; margin-bottom: 0">06:10:30 01/09/2007
0004
U02
1</p>
<p style="margin-top: 0; margin-bottom: 0">08:10:10 01/09/2007
0006
U02
1</p>
<p> </p>
<p>I need to delete the row in table1 and leave the minimum <b>timein</b> in
that table by <b>useridin</b>.</p>
<p>The last output in table1 is like below.</p>
<p style="margin-top: 0; margin-bottom: 0"><b>Timein
DateIn
locationin useridin
status</b></p>
<p style="margin-top: 0; margin-bottom: 0">07:20:00 01/09/2007
0006
U01
1</p>
<p style="margin-top: 0; margin-bottom: 0">06:10:30 01/09/2007
0004
U02
1</p>
<p>Can i used below sql to output the result;</p>
<font FACE="Courier" SIZE="2">
<p style="margin-top: 0; margin-bottom: 0"></font>
<font FACE="Courier" SIZE="2" COLOR="#0000ff">delete</font><font FACE="Courier" SIZE="2">
</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">from</font><font FACE="Courier" SIZE="2">
<font color="#808000">table1</font> t1 </font>
<font FACE="Courier" SIZE="2" COLOR="#0000ff">where</font><font FACE="Courier" SIZE="2">
t1</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">.</font><font FACE="Courier" SIZE="2" COLOR="#ff0000">rowid</font><font FACE="Courier" SIZE="2">
</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">></p>
</font><font FACE="Courier" SIZE="2">
<p style="margin-top: 0; margin-bottom: 0"></font>
<font FACE="Courier" SIZE="2" COLOR="#0000ff">(</font><font FACE="Courier" SIZE="2">
</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">select</font><font FACE="Courier" SIZE="2">
</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">min(</font><font FACE="Courier" SIZE="2">t2</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">.</font><font FACE="Courier" SIZE="2" COLOR="#ff0000">rowID</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">)</font><font FACE="Courier" SIZE="2">
</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">from</font><font FACE="Courier" SIZE="2">
<font color="#808000">table1</font> t2</p>
<p style="margin-top: 0; margin-bottom: 0"></font>
<font FACE="Courier" SIZE="2" COLOR="#0000ff">where</font><font FACE="Courier" SIZE="2">
t1</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">.</font><font face="Courier" size="2">Datein
= t2.Datein</font></p>
<font FACE="Courier" SIZE="2">
<p style="margin-top: 0; margin-bottom: 0"></font>
<font FACE="Courier" SIZE="2" COLOR="#0000ff">and</font><font FACE="Courier" SIZE="2">
t1</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">.</font><font FACE="Courier" SIZE="2">Status
</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">=</font><font FACE="Courier" SIZE="2">
t2</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">.</font><font FACE="Courier" SIZE="2">Status</font></p>
<p style="margin-top: 0; margin-bottom: 0">
<font FACE="Courier" SIZE="2" COLOR="#0000ff">and</font><font FACE="Courier" SIZE="2">
t1</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">.</font>UserIdin
<font FACE="Courier" SIZE="2" COLOR="#0000ff">=</font><font FACE="Courier" SIZE="2">
t2</font><font FACE="Courier" SIZE="2" COLOR="#0000ff">.</font>UserIdin
<font FACE="Courier" SIZE="2" COLOR="#0000ff">order by t2.timein);</font></p>
<p style="margin-top: 0; margin-bottom: 0"> </p>
<p style="margin-top: 0; margin-bottom: 0"><font face="Courier" size="2">Thanks
in advancedHi Rob,
Just trying to chip in little bit!
Created Table As follows same as you, only two rows with duplicatem user, datein, and timein:
create table table1
as
select '08:20:00' timein, date '2007-09-01' datein, '0001' locationin, 'U01' useridin, 1 status from dual union all
select '08:10:30', date '2007-09-01', '0004', 'U01', 1 from dual union all
select '07:20:00', date '2007-09-01', '0006', 'U01', 1 from dual union all
select '08:14:00', date '2007-09-01', '0001', 'U02', 1 from dual union all
select '06:10:30', date '2007-09-01', '0004', 'U02', 1 from dual union all
select '06:10:30', date '2007-09-01', '0001', 'U02', 1 from dual
If I run your delete statement result row will be as follows:
SQL> select * from table1;
TIMEIN DATEIN LOCA USE STATUS
07:20:00 01-SEP-07 0006 U01 1
06:10:30 01-SEP-07 0004 U02 1
06:10:30 01-SEP-07 0001 U02 1
Here problem is that its not able to delete duplicate rows having same timein and timeout.
So I changed you query as follows:
DELETE FROM tab1
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM tab1
WHERE (UseridIn, TO_DATE(TO_CHAR(DateIn,'YYYYMMDD') || TimeIn,'YYYYMMDDHH24:MI:SS')) IN
( SELECT UseridIn, MIN(TO_DATE(TO_CHAR(DateIn,'YYYYMMDD') || timein,'YYYYMMDDHH24:MI:SS'))
FROM tab1
GROUP BY UseridIn)
GROUP BY UseridIn
If I run your delete statement with little bit of changes, the result row was as follows:
SQL> select * from table1;
TIMEIN DATEIN LOCA USE STATUS
07:20:00 01-SEP-07 0006 U01 1
06:10:30 01-SEP-07 0004 U02 1
Regards,
Raj -
How to delete duplicate rows in oracle and retaining last duplicate rows
Hi,
I'm having millions of records in the table .Some duplicate values/rows are inserted in it.
I just want to delete duplicate rows but also want to retain the last duplicate row.
For eg if one record is found three times ,i want to delete first and second record and retain
the third one i.e the last inserted one.
Regards
Paramdeep Singhuser13310594 wrote:
Hi,
I'm having millions of records in the table .Some duplicate values/rows are inserted in it.
I just want to delete duplicate rows but also want to retain the last duplicate row.
For eg if one record is found three times ,i want to delete first and second record and retain
the third one i.e the last inserted one.Hi Paramdeep,
To start with, since you do not wish to keep multiple rows with same values, why allow them to get inserted in the first place?
Wouldn't it be easier to block duplicate rows from inserting by creating a Unique constraint on Column(s) that define a row as duplicate, then rather deleting duplicate rows periodically?
For deleting duplicate rows, there are N number of techniques available. Always remember, you need to have a rigid criteria that marks row as duplicate.
[url http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:15258974323143]this is one recomended by tom for large tables.
Vivek L -
How to identify database records without using rowid.
Hello,
I want to find a way to identify records uniquely in my database without the use of the rowid.
What I want to do is the following :
I have a table that contains images. Something like that :
Table images (id number, image blob).
Lets say that in this table I have scanned images of persons, invoices and orders.
I want to be able to associate a record with different records from different tables from my ERP via an association table.
this table could be :
Table image_associations (imageid number, rowid_column varchar2(100))
The "rowid_column" would contain the rowid of the row that is associated with the image.
The problem is that the rowid is something that changes (for example after export/import).
So I do not want to use rowid.
Any ideas?
ThanksWhat you are saying is you want to avoid theaccepted relational design theory and subvert the
fundamental principles of very model?
Yes !!!!Then you are very short sighted and will fail. Why would you want to do this? How is it that you think you are smarter than the forefathers of our industry? an idea so compelling that huge companies have many massive profits implementing the concepts? (sometimes even properly) and how is it that you imagine you can work outside the concepts underlying those tools, within the tool and come to a better solution? this is arrogance surely.
>
I think a lot of people use rowid to identify within
plsql code a record instead of using the table-key.
There is very limited specific application of the ROWID for temporary reference within known bounds.
You shouldn't rely on the ROWID. Think about what it actually is: a physical location address hash.
Don't use it. Just don't.
As I can remember from my classrooms (a lot of years
ago) rowid was never mentioned in ER models. Still
that helps a lot..For very good reason. Rowid is an implementation concept, and not a logical concept.
Oracle already makes provision at the physical storage level to store BLOBs out of line with a record. You don't need to try to do this at the logical level as I said already. -
How to delete empty row without validation error in ADF Table(EMP)
Hi Everyone,
I am using EMP Table in ADF jspx page to insert the data into database.when i insert a row into table by createInsert operation,it inserting the row.But I need to delete that row immediately with out entering any value.
But it showing some validation error at empno.Is there any ways to delete the empty row?if not,what are the reasons that we can't delete the row.
could any one tell me the reasons!!
Thanks in advance!!
With Best Regards,
Amar
Edited by: 973755 on Dec 11, 2012 6:42 AMAmar,
I am little confused with your logic here.....
but if you are trying to remove the row by clicking Remove button, you can set the immediate property to true and that remove function will run without executing any entity validation.......
-R -
How to delete duplicate rows in a table
hai,
i have a table which consists of hunderds of records and contains duplicate records also. how can i delete duplicate records.
thanks
raviThis maybe a litle bit fast for deletion:-
delete from emp where rowid in (
2 SELECT rowid FROM emp
3 group by rowid,empno,ename,job
4 minus
5 SELECT min(rowid) FROM emp
6 group by empno,ename,job); -
How can I easily delete all duplciate photos from my entire library? I really don't want to do this manually as I have many photos. When I did my sync with camera and photos using my iPhone and iPad many of my photos were duplicated. Thanks
Photos does not have a built-in duplicate remover.
Try to restore the Photos library from before you synced and duplicated the photos from your backup.
Most third party duplicate cleaners do not yet work with Photos, but there will be soon one that works with Photos:
ttp://www.fatcatsoftware.com/comments/announcing_powerphotos
Also, Duplicate Annihilator is now available for Photos. -
How to delete video podcast without using iTunes
My Mac is G5 PPC, OSX 10.4.11, so cannot use
iTunes to sync Ipad; so is there a way of deleting downloaded video podcasts from the iPad?
When headphone is connected to the iPad, should the internal speaker not be automatically disconnected like on the iPod?makam wrote:
Appreciate suggestion, but does not work. Maintaining touch on the title, highlight the video, no delete check mark appears; letting go start the video playing again.
Kappy's suggestion doesn't work for me either. Just go into the iPod App, click on Podcasts, and swipe to the right. You should get a "Delete" block on the right. That's how it works on my iPad. -
As per MSDN, we cannot use AppointmentItem.Delete() inside Close() event handler method of AppointmentItem. I need to find a way to delete this AppointmentItem (or preventing it from getting saved to Outlook calender). Please let me know if you have a way
to do this.
Thanks in advance.
PrasadHello Prasad,
You can run a timer or use any other suitable event handler. In the timer' tick event handler you can get an instance of the AppointmentItem class by using the GetItemFromId method of the Namespace class. You just need to pass an entryId value which you
may grab in the Close event handler.
Be aware, you need to use the Timer class which uses the single thread for their events. You should access Outlook objects on the main thread only. -
Select extra row without using UNION ALL in pl/sql
Hi,
Can anyone tell me how to select extra row without using UNION or UNION ALL in pl/sql. Actually I want to have my o/p of query as partitioned by designation and ordered by salary and than one extra row which will contain the highest salary in a particular salary. My table has first_name,emp_id,designation and salary column. And I wnt the o/p as.
Mohinish,12212,SI,46000
Ram,11212,SSI,47000
Shyam,12133,SI,48000
Rick,9898,SI,46000
Rocky,12312,SSI,56000
Sariq,23948,SI,43000
Suman,12789,HR,49000
Sampy,12780,SI,46000
Parna,11111,HR,50000
Now the o/p should be.
Mohinish,12212,SI,46000
Rick,9898,SI,46000
Sariq,23948,SI,43000
Shyam,12133,SI,48000
Shyam,12133,SI,48000
Ram,11212,SSI,47000
Rocky,12312,SSI,56000
Rocky,12312,SSI,56000
Suman,12789,HR,49000
Parna,11111,HR,50000
Parna,11111,HR,50000
Thanks in AdvanceYou don't have to do a UNION or UNION ALL in PL/SQL but you would need to in SQL to get the desired output:
with data_recs
as (select 'Mohinish' first_name,12212 emp_id,'SI' designation,46000 salary from dual union
select 'Ram',11212,'SSI',47000 from dual union
select 'Shyam',12133,'SI',48000 from dual union
select 'Rick',9898,'SI',46000 from dual union
select 'Rocky',12312,'SSI',56000 from dual union
select 'Sariq',23948,'SI',43000 from dual union
select 'Suman',12789,'HR',49000 from dual union
select 'Sampy',12780,'SI',46000 from dual union
select 'Parna',11111,'HR',50000 from dual)
select first_name, emp_id, designation, salary from data_recs union all
select s.first_name, s.emp_id, s.designation, s.salary
from (select first_name,
emp_id,
designation,
salary,
row_number() over (partition by designation order by salary desc) high_salary
from data_recs
order by designation, salary) s
where s.high_salary = 1
order by designation, salary;
FIRST_NAME EMP_ID DESIGNATION SALARY
Suman 12789 HR 49000
Parna 11111 HR 50000
Parna 11111 HR 50000
Sariq 23948 SI 43000
Rick 9898 SI 46000
Mohinish 12212 SI 46000
Sampy 12780 SI 46000
Shyam 12133 SI 48000
Shyam 12133 SI 48000
Ram 11212 SSI 47000
Rocky 12312 SSI 56000
Rocky 12312 SSI 56000 -
How to delete duplicate songs from libary without deleting them from playlist or other devices
I'm with you verm71; it's not entirely clear how Apple is expecting you to manage this. I'm going on vacation soon and want to clean up my phone so that it's nice and empty for the new pictures. There doesn't seem to be a way to do this without losing your pictures in the new Photos as well. If imported, it's impossible to see which is the Cloud version and which is the Local version. It's very annoying.
I've also noticed in iPhoto you used to have the ability to locate the original of the picture inside the photo database. That option is not provided in Photos, making finding the original extremely difficult.
It seems to be a great, well-oiled system. But I would love to know how Apple envisions the typical workflow for archiving... -
Delete duplicate rows -- based on 4 columns -- how?
I asked this question on how to delete duplicates recently and received this suggestion which works well -- except --
With CTE AS
SELECT *, ROW_NUMBER() OVER (Partition by fld1 order by fld1) RowNum
FROM #tmpA
DELETE From CTE
WHERE RowNum > 1
-- the actual table I need to delete duplicate rows on is based on 4 columns. The following table contains 14,462 rows of which 14,348 are distinct -- based on the following 4 colums. Below is an image of a sample of the data contained in the
table for my question and to the right of that data is the column structures (data types). Is it possible to do something like the above example suggestion with the table in the image below? How to do that? I need to delete rows so that 14462
goes down to 14348. If I only reference one column for the delete -- this would delete like 7000+ rows. I only need to remove 114 rows.
Rich PAdd the other 3 columns to the partition.
Jason Long -
How to delete duplicate records in 10 G.
how to delete duplicate records in 10 G.
--Here is one way to do it using a second table
create table temp1
(col1 char(1));
--Table created.
insert into temp1 (col1) values('A');
insert into temp1 (col1) values('B');
insert into temp1 (col1) values('B');
--1 row created.
--1 row created.
--1 row created.
create table temp2 as select distinct * from temp1;
--Table created.
--now you have a second table with no duplicates
--truncate your old table
truncate table temp1;
--Table truncated.
--and reload it with data from the new table
insert into temp1 select * from temp2;
--2 rows created.
--then drop the temp2 table
drop table temp2 -
Deleting duplicate rows based on three columns in Oracle 8i
Hi all,
The database we use is Oracle 8i.
The query below raises the too_many_rows exception when I launch an application. So I want to delete the duplicated rows :
select polarisation_1, polarisation_2
into v_pol1_tech, v_pol2_tech
from v_cfh_lien_element
where nom_lien = p_nom_lien
AND num_canal_1 = p_num_canal_1
AND freq_emise_1 = p_freq_emise_1;
Notice that with many possible values of the parameters p_nom_lien, p_num_canal_1 and p_freq_emise_1 then the exception is raised.
So how to delete generally the duplicated rows based on the three columns "nom_lien" , "num_canal_1" and "freq_emise_1" ?
Thank you very much indeed.Check the other thread with same question deleting duplicate rows based on three columns in Oracle 8i
-
SQL Server 2014 - Delete duplicate rows in a table except one
Hi all,
I have a table that contains duplicate rows. Using grouping I can easily identify those rows.
As of now, I count the grouped records and copy one of the group in a temporary table together with its count of elements. Than I loop over this table and delete N - 1 rows of each group from the original table. Unfortunately this is done with two cursors
and WHILE loops.
Is anybody out there who could give some hints how to handle deletion of duplicated rows leaving one in place using pure SQL? I tried a full delete of all elements of a group and then an INSERT of one row to recreate a row again. But this is not
very elegant and close to using cursors.
Regards UweHi all,
based on input below from Uri, I have developed a little more general solution for removal of duplicates. I did that, because I'm sure that for many beginners in SQL it is easier specialize a proposal by removing parts of it than adding new functionality.
Here is my code.
-- Remove duplicates from a table and document the deleted rows
DECLARE @MyTable Table (Col1 int, Col2 int, Col3 int, Col4 int);
DECLARE @OutTable Table (Col1 int, Col2 int, Col3 int, Col4 int);
INSERT INTO @MyTable VALUES
(1, 10, 100, 7),
(2, 20, 200, 2),
(2, 20, 200, 1),
(3, 30, 300, 3),
(1, 10, 200, 4),
(1, 10, 100, 5),
(1, 10, 100, 6);
WITH cte (RN, Col1, Col2, Col3, Col4)
AS (SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3 ORDER BY (SELECT 0)) AS RN, Col1, Col2, Col3, Col4
FROM @MyTable)
DELETE FROM cte
OUTPUT DELETED.Col1, DELETED.Col2, DELETED.Col3, DELETED.Col4
INTO @OutTable (Col1, Col2, Col3, Col4)
WHERE RN > 1;
SELECT * FROM @OutTable;
And this is the result:
The MS 139444 article Satish mentioned is focussed on duplicate PKs removal. Thanks for the hint.
Thanks to you all for your rapid response.
Regards Uwe
Maybe you are looking for
-
Accruals Condition Record posted late in COPA along with the Settlement
Hi Gurus, Accruals condition record for 1 item in an Invoice is posted along with the Settlement Credit Note in COPA. While condition records of the other items from the same invoice were posted on the Billing Date itself. For eg: Invoice 111 D
-
When I scroll to view names in my address book the list display becomes exceedingly distorted and "smeary". This only happens in the "name" column. It's so bad that the only way I can find an entry in my address book is to search for the name within
-
Hi Concerning a HP LaserJet 600 M602x. Is it possible to set the I/O timeout through any remote session, e.g. web page, telnet or WebJet? On my HP LaserJet 9050dn it can be done using the web page, but I can for the life of me not find it on the M602
-
Hey everyone, I just installed Windows 7 Ultimate x64 on my Macbook Pro and it works just fine, but I have one issue with it. Basically, for about a minute an a half after selecting Windows to boot, I get nothing but a black screen with a white curso
-
HT201210 Facetime is not there when i updated my 4s using wifi to ios6,why?
I bought recently 4s and updated the software by going settings,general then update software. Updating takes about two hour after down loading a pop message came out saying there was problem but i just closed it and click Install,my iphone succesfull