1

my problem is trying to use a SELECT statement and order the top 10 by a certain column. I managed to compile something after searching through lots of forums, however I need to confirm that the timestamp in one field is within the last week. I have gotten this to execute however i'm not sure whether this is correct as I can't print the value for the where clause:

SELECT itemid, count(itemid) 
FROM Rateddate 
WHERE TO_CHAR(CURRENT_TIMESTAMP - DATE_RATED) < TO_CHAR(7)  
GROUP BY itemid;

TLDR:

TO_CHAR(CURRENT_TIMESTAMP - DATE_RATED) < TO_CHAR(7)

does this make sure the date_rated timestamp is less than a week old?

2 Answers 2

4

It would make more sense to say

WHERE date_rated > sysdate - interval '7' day

if you want data from the last 168 hours. You may want

WHERE date_rated > trunc(sysdate) - interval '7' day

if you want data from any point in the day 7 days ago rather than caring about what time of day it is currently.

Sign up to request clarification or add additional context in comments.

Comments

0

Wouldn't this work for you:

  trunc(CURRENT_TIMESTAMP - DATE_RATED) < 7

This is assuming that DATE_RATED is a date field. If not, you need to convert it first with TO_DATE().

2 Comments

this throws ORA-00932: inconsistent datatypes: expected NUMBER got INTERVAL DAY TO SECOND
If this is a timestamp filed then you need to cast to a date. What is your table definition?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.