Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Traversing back 3 days (window) in SQL

Kuldeep2Jun 7 2010 — edited Nov 3 2010
create table t ( id integer, dt date)
insert into t values (1,sysdate);
insert into t values (2,sysdate-1);
insert into t values (3,sysdate-2);
insert into t values (4,sysdate-3);
insert into t values (5,sysdate-4);
insert into t values (6,sysdate-5);
insert into t values (8,sysdate-7);

I have data as follows.
ID	DT
++++++++++++++++++++++++
1	07-JUN-10
2	06-JUN-10
3	05-JUN-10
4	04-JUN-10
5	03-JUN-10
6	02-JUN-10
8	31-MAY-10
Now i need to write a query to return data which comes under 3 days (thois could be 4- its a user input) window.

e.g.

Query will start from 07 June and will go back 3 days i.e. till 4th June, now since there is data for 4th june it will go back till 1st June. Now we do not have data for 1-Jun so it will stop here and query will return all record except for 31st May.

I do not have choice of writing procedure here. can someone help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 5 2010
Added on Jun 7 2010
11 comments
3,186 views