SELECT using timestamp in Oracle SQL - sql

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?

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.

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().

Related

how to get data for last calender week in redshift

I have a below query that I run to extract material movements from the last 7 days.
Purpose is to get the data for the last calender week for certain reports.
select
*
From
redshift
where
posting_date between CURRENT_DATE - 7 and CURRENT_DATE - 1
That means I need to run the query on every Monday to get the data for the former week.
Sometimes I am too busy on Monday or its vacation/bank holiday. In that case I would need to change the query or pull the data via SAP.
Question:
Is there a function for redshift that pulls out the data for the last calender week regardless when I run the query?
I already found following solution
SELECT id FROM table1
WHERE YEARWEEK(date) = YEARWEEK(NOW() - INTERVAL 1 WEEK)
But this doesnt seem to be working for redshift sql
Thanks a lot for your help.
Redshift offers a DATE_TRUNC('week', datestamp) function. Given any datestamp value, either a date or datetime, it gives back the date of the preceding Sunday.
So this might work for you. It filters rows from the Sunday before last, up until but not including, the last Sunday, and so gets a full week.
SELECT id
FROM table1
WHERE date >= DATE_TRUNC('week', NOW()) - INTERVAL 1 WEEK
AND date < DATE_TRUNC('week', NOW())
Pro tip: Every minute you spend learning your DBMS's date/time functions will save you an hour in programming.

Trying to accommodate relative defined date, such as 5 days ago, into my fixed date condition in PostgreSQL

I'm trying to condition my WHERE clause to accommodate relatively defined dates into my date filter. I'm pretty confused what type I need to use, if it's CONVERT or TO_DATE function, or if I need to put a CASE WHEN statement into my code.
This is the code that I have written so far:
WHERE event_create_verified_user_success.created_at_utc_date
BETWEEN DATE '2021-11-29' AND DATE '2021-12-05'
And this is the condition of the activity I need to finish:
If the desired date-period is not set manually using fixed dates like from “2021-11-29”
to “2021-12-05”, how would you change the where-clause to consider all data from relative
defined dates: “consider messages created between 10 days and 5 days ago (inclusive)”
I've only started PostgreSQL yesterday and the last time I've handle SQL was probably 4 years ago so I'm pretty confused at how much SQL has changed since then.
Thank you so much for helping!
The basic syntax hasn't really changed in the last 4 years (or even 15 years).
You can use current_date to obtain "today's date". You can subtract days from that
where ... between current_date - 10 and current_date - 5
If created_at_utc_date is a timestamp (= date and time) rather than a date (=date without time) it's better to use a range query though:
where created_at_utc_date >= current_date - 10
and created_at_utc_date < current_date - 4
Note the < combined with the next day you want to compare with.

Date Functions Trunc (SysDate)

I am running the below query to get data recorded in the past 24 hours. I need the same data recorded starting midnight (DATE > 12:00 AM) and also data recorded starting beginning of the month. Not sure if using between will work or if there is better option. Any suggestions.
SELECT COUNT(NUM)
FROM TABLE
WHERE
STATUS = 'CNLD'
AND
TRUNC(TO_DATE('1970-01-01','YYYY-MM-DD') + OPEN_DATE/86400) = trunc(sysdate)
Output (Just need Count). OPEN_DATE Data Type is NUMBER. the output below displays count in last 24 hours. I need the count beginning midnight and another count starting beginning of the month.
The query you've shown will get the count of rows where OPEN_DATE is an 'epoch date' number representing time after midnight this morning*. The condition:
TRUNC(TO_DATE('1970-01-01','YYYY-MM-DD') + OPEN_DATE/86400) = trunc(sysdate)
requires every OPEN_DATE value in your table (or at least all those for CNLD rows) to be converted from a number to an actual date, which is going to be doing a lot more work than necessary, and would stop a standard index against that column being used. It could be rewritten as:
OPEN_DATE >= (trunc(sysdate) - date '1970-01-01') * 86400
which converts midnight this morning to its epoch equivalent, once, and compares all the numbers against that value; using an index if there is one and the optimiser thinks it's appropriate.
To get everything since the start of the month you could just change the default behaviour of trunc(), which is to truncate to the 'DD' element, to truncate to the start of the month instead:
OPEN_DATE >= (trunc(sysdate, 'MM') - date '1970-01-01') * 86400
And the the last 24 hours, subtract a day from the current time instead of truncating it:
OPEN_DATE >= ((sysdate - 1) - date '1970-01-01') * 86400
db<>fiddle with some made-up data to get 72 back for today, more for the last 24 hours, and more still for the whole month.
Based on your current query I'm assuming there won't be any future-dated values, so you don't need to worry about an upper bound for any of these.
*Ignoring leap seconds...
It sounds like you have a column that is of data type TIMESTAMP and you only want to select rows where that TIMESTAMP indicates that it is today's date? And as a related problem, you want to find those that are the current month, based on some system values like CURRENT TIMESTAMP and CURRENT DATE? If so, let's call your column TRANSACTION_TIMESTAMP instead of (reserved word) DATE. Your first query could be:
SELECT COUNT(NUM)
FROM TABLE
WHERE
STATUS = 'CLND'
AND
DATE(TRANSACTION_TIMESTAMP)=CURRENT DATE
The second example of finding all for the current month up to today's date could be:
SELECT COUNT(NUM)
FROM TABLE
WHERE
STATUS = 'CLND'
AND
YEAR(DATE(TRANSACTION_TIMESTAMP)=YEAR(CURRENT DATE) AND
MONTH(DATE(TRANSACTION_TIMESTAMP)=MONTH(CURRENT DATE) AND
DAY(DATE(TRANSACTION_TIMESTAMP)<=DAY(CURRENT DATE)

SQL searching in 2 week interval from the current date

I'm having trouble setting up a SQL to bring up all information 2 weeks before and after the current date. Here is what I am currently doing:
Select WRK.Wrk, WRK.Client, WRK.Status, WRK.TAT, WRK.Due
From WRK
WHERE WRK.Due >= now()
Order By WRK.Due Desc, WRK.Status Desc
This gets me everything due on or after the current date but when I try to add lines to indicate 2 weeks before and after the current date I get errors.
Thanks
Date/time function vary significantly among databases. The use of now() makes me think of MySQL. The syntax in MySQL is:
where wrk.Due between date_sub(curdate(), interval 2 weeks) and date_add(curdate, interval 2 weeks)
Note that between includes the end dates, so this might be off by a day in either direction.
You can implement similar logic in other databases, but the specific functions would look different.

Is there a way to search a SQL Query with Month Numbers, and not Month Name?

I am trying to use the actual numerical value for the month on a sql query to pull results. Is there any way to do this without having a function to change the numbers to actual month names, then back to month numbers? The following code works for Names, what works for numbers?
datename(month,(convert(datetime,DTSTAMP)))=
'October'
month,(convert(datetime,DTSTAMP)) should do it, but why on earth are you not storing the data correctly as a datetime to begin with? All that additional conversion stuff to use the dates adds unnecessary load to your server and slows down your application.
Datepart is an alternative to the month command and it is more flexable as you can extract other parts of the date.
DATEPART(mm, convert(datetime,DTSTAMP))
Gets Month Number of Date
CONVERT(VARCHAR(2) ,DTSTAMP ,110)
here, you can search with diff options like:
DATE_SUB(CURDATE(), INTERVAL 0 DAY)
DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
as per your requirement.