analytic function [message #415314] |
Mon, 27 July 2009 04:21 |
cherry
Messages: 56 Registered: December 2007
|
Member |
|
|
hi all
im posting only a relevant portion of my prob.
i know its simple, but i need pointers or help to get this through.
create table test2 (creation_date date);
insert into test2 values (to_date('17-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('17-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('20-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('20-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('20-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('21-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('21-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('21-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('21-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('22-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('22-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('23-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('23-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('23-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('24-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('24-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('27-jul-2009', 'dd-mon-yyyy'));
insert into test2 values (to_date('27-jul-2009', 'dd-mon-yyyy'));
select * from test2;
CREATION_DATE
-------------
17/07/2009
17/07/2009
20/07/2009
20/07/2009
20/07/2009
21/07/2009
22/07/2009
22/07/2009
21/07/2009
21/07/2009
21/07/2009
23/07/2009
23/07/2009
23/07/2009
24/07/2009
24/07/2009
27/07/2009
27/07/2009
at any point of time, i should have latest 2 days old data.
how do i delete data NOT IN ('27-JUL-2009', '24-JUL-2009')?
in this case the 3rd latest date is '23-JUL-2009'. but the 3rd latest date can be any date, even a month / year old date.
after delete the data should be ..
CREATION_DATE
-------------
24/07/2009
24/07/2009
27/07/2009
27/07/2009
thanks
|
|
|
|
|
Re: analytic function [message #415500 is a reply to message #415318] |
Tue, 28 July 2009 01:55 |
cherry
Messages: 56 Registered: December 2007
|
Member |
|
|
im new to analytics. this is what i've got.
SELECT creation_date,
DENSE_RANK () OVER (PARTITION BY NULL ORDER BY creation_date)
FROM test2
CREATION_DATE RANK
------------- ----
17/07/2009 1
17/07/2009 1
20/07/2009 2
20/07/2009 2
20/07/2009 2
21/07/2009 3
21/07/2009 3
21/07/2009 3
21/07/2009 3
22/07/2009 4
if the first step above is correct, how to form the delete?
thanks
|
|
|
|
|
Re: analytic function [message #415513 is a reply to message #415509] |
Tue, 28 July 2009 02:19 |
cherry
Messages: 56 Registered: December 2007
|
Member |
|
|
thanks Michel.
but im not able to form the delete with analytic.
i know i have to delete rows less than what i get from the below select.
SELECT MAX (RANK) - 1
FROM (SELECT creation_date,
DENSE_RANK () OVER (PARTITION BY NULL ORDER BY creation_date)
RANK
FROM test2
ORDER BY RANK DESC)
[Updated on: Tue, 28 July 2009 02:31] Report message to a moderator
|
|
|
|
|
Re: analytic function [message #415555 is a reply to message #415314] |
Tue, 28 July 2009 04:12 |
alammas
Messages: 46 Registered: July 2008
|
Member |
|
|
Check it.
delete from test2
where creation_date not in
(SELECT creation_date FROM
( SELECT creation_date , RANK() OVER (ORDER BY creation_date DESC ) date_rank
FROM test2 group by creation_date)
WHERE date_rank <= 2 )
/
|
|
|
|