Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with basic sql oracle queries?

Newbie
 
Join Date: Aug 2009
Posts: 1
#1: Aug 16 '09
I've got a table, lets say table name is A and inside there's "date" column and "order name" column. How to retrieve the order for the last 45 days without knowing what're the dates inside the column?

OraMaster's Avatar
Member
 
Join Date: Aug 2009
Location: Pune, India
Posts: 76
#2: Aug 17 '09

re: Need help with basic sql oracle queries?


Quote:

Originally Posted by paganini View Post

I've got a table, lets say table name is A and inside there's "date" column and "order name" column. How to retrieve the order for the last 45 days without knowing what're the dates inside the column?

Pls check below SQL

Expand|Select|Wrap|Line Numbers
  1. SELECT ROWNUM, od.*
  2.   FROM (SELECT   *
  3.             FROM orderdtls
  4.         ORDER BY orderdate DESC) od
  5.  WHERE orderdate <= SYSDATE AND ROWNUM <= 45
Let me know if any issues with above SQL.
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#3: Aug 17 '09

re: Need help with basic sql oracle queries?


Try This:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM order_details WHERE TRUNC(order_date) >= TRUNC(SYSDATE) - 45;
  3.  
  4.  
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#4: Aug 17 '09

re: Need help with basic sql oracle queries?


Quote:

Originally Posted by OraMaster View Post

Pls check below SQL

Expand|Select|Wrap|Line Numbers
  1. SELECT ROWNUM, od.*
  2.   FROM (SELECT   *
  3.             FROM orderdtls
  4.         ORDER BY orderdate DESC) od
  5.  WHERE orderdate <= SYSDATE AND ROWNUM <= 45
Let me know if any issues with above SQL.

why use rownum ?

Question is not to retrieve last 45 records but of data of last 45 days.
There may be more that one entry in the database per day.

How rownum will help in that scenario ?
Reply