Delete even rows of a table [message #199567] |
Wed, 25 October 2006 02:10  |
mr_deb
Messages: 3 Registered: October 2006
|
Junior Member |
|
|
hi
Can anyone tell me how to delete even rows of a table or rather alternate rows of a table.
Waiting for your reply.Thanx in advance.
|
|
|
|
|
Re: Delete even rows of a table [message #199607 is a reply to message #199576] |
Wed, 25 October 2006 04:20   |
 |
Littlefoot
Messages: 21824 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
ROWNUM is a pseudocolumn which shows row number of the fetched record. MOD(rownum, 2) is equal to 0 when rownum is an even number (and is equal to 1 when rownum is an odd number). So, it deletes all records where ROWNUM is even.SQL> select rownum, mod(rownum, 2) mod_2, ename
2 from emp
SQL> /
ROWNUM MOD_2 ENAME
---------- ---------- ----------
1 1 SMITH
2 0 ALLEN
3 1 BURGLAR
4 0 JONES
5 1 TIGER
6 0 BLAKE
7 1 CLARK
8 0 SCOTT
9 1 KING
10 0 TURNER
11 1 ADAMS
12 0 JAMES
13 1 FORD
14 0 MILLER
[EDIT] Added a screen output
[Updated on: Wed, 25 October 2006 04:23] Report message to a moderator
|
|
|
|
Re: Delete even rows of a table [message #199639 is a reply to message #199624] |
Wed, 25 October 2006 10:02   |
skooman
Messages: 913 Registered: March 2005 Location: Netherlands
|
Senior Member |
|
|
Can't help but asking: what on earth can be the functional requirement to remove even rows?
(please mind that the order in which Oracle saves rows in a table is not known or effected by developers, users or anyone).
Regards,
Sabine
[Updated on: Wed, 25 October 2006 10:03] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Delete even rows of a table [message #575551 is a reply to message #575545] |
Thu, 24 January 2013 01:28   |
 |
Michel Cadot
Messages: 68760 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
MAHESH1244 wrote on Thu, 24 January 2013 06:58WITH TEMP AS
(
SELECT ROW_NUMBER() OVER (ORDER BY EMPID) AS ROWNUM,* FROM EMPLOYEE
)DELETE FROM TEMP WHERE ROWNUM%2=0
SQL> WITH TEMP AS
2 (
3 SELECT ROW_NUMBER() OVER (ORDER BY EMPID) AS ROWNUM,* FROM EMPLOYEE
4 )DELETE FROM TEMP WHERE ROWNUM%2=0
5 /
SELECT ROW_NUMBER() OVER (ORDER BY EMPID) AS ROWNUM,* FROM EMPLOYEE
*
ERROR at line 3:
ORA-00923: FROM keyword not found where expected
Please TEST your query before posting silly things.
Even if you remove the reserved word it is wrong:
SQL> WITH TEMP AS
2 (
3 SELECT ROW_NUMBER() OVER (ORDER BY EMPID) AS R,* FROM EMPLOYEE
4 )DELETE FROM TEMP WHERE R%2=0
5 /
SELECT ROW_NUMBER() OVER (ORDER BY EMPID) AS R,* FROM EMPLOYEE
*
ERROR at line 3:
ORA-00936: missing expression
Even if you fix the error it is wrong:
SQL> WITH TEMP AS
2 (
3 SELECT ROW_NUMBER() OVER (ORDER BY EMPID) AS R,employee.* FROM EMPLOYEE
4 )DELETE FROM TEMP WHERE R%2=0
5 /
)DELETE FROM TEMP WHERE R%2=0
*
ERROR at line 4:
ORA-00928: missing SELECT keyword
And so on.
Regards
Michel
|
|
|
|
Re: Delete even rows of a table [message #575581 is a reply to message #199643] |
Thu, 24 January 2013 07:58   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
skooman wrote on Wed, 25 October 2006 11:13Maybe a new contest: find ways to randomly delete half of the records in a table
delete your_table
where rowid in (
with t as (
select rowid rid,
ceil(count(*) over() / 2) half_cnt,
row_number() over(order by dbms_random.value) rn
from your_table
)
select rid
from t
where rn <= half_cnt
)
/
SY.
[Updated on: Thu, 24 January 2013 08:00] Report message to a moderator
|
|
|
Re: Delete even rows of a table [message #575586 is a reply to message #575581] |
Thu, 24 January 2013 08:44   |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
ok here is my version:
DELETE your_table
WHERE rowid in
(SELECT rid
FROM
(SELECT rowid rid, ntile (2) over (order by 1) tile
FROM your_table) t2
WHERE tile=2
);
ADD: Experts would use
(SELECT rowid rid, ntile (2) over (order by mod(rownum+1,2)) tile
to DELETE only the EVEN rownum
[Updated on: Thu, 24 January 2013 09:07] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|