records from yesterday in postrgesql - postgresql

I have an application in which I've used MySQL. I had a report that stretched records of the last 24 hours. I used the query:
WHERE (DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= FROM_UNIXTIME(`workorder`.`CREATEDTIME` / 1000))
Now I have to use PostgreSQL and do not know how to make a report of the last 24 hours.
Can any of you help?

WHERE workorder.createdtime > current_date - 1 -- Yesterday and today
WHERE workorder.createdtime > current_timestamp - interval '1 day' -- last 24hr

> TIMESTAMP 'yesterday'
For convenience, Postgres includes a few hard-coded values as special Date/Time inputs. They include:
yesterday
today
tomorrow
now
Try SELECT TIMESTAMP 'now'.
For example, here is a query.
SELECT when_row_created_
FROM customer_
WHERE when_row_created_ > TIMESTAMP 'yesterday'
ORDER BY when_row_created_ DESC
;
These commands may not be appropriate to production code, but they certainly are handy in development. Read the docs and do some practice to be sure you understand the behavior of these commands, how the session’s time zone affects them and so on.
Downsides include (a) implicitly ignoring the crucial issue of time zone, and (b) not standard SQL.

where workorder.createdtime >= now() - interval '24 hour'

WHERE workorder.createdtime::date = current_date - 1; --from yesterday

Related

Extracting Hour and Minutes In RedShift Within Same Function

I am trying to find rows that came in before 7:30 pm (as part of a CASE WHEN). The below just has it at 8. Any idea how to get this to be 7:30?
extract(dayofweek from convert_timezone('EST', call_start_time)) in (6) and
extract(hour from convert_timezone('EST', call_start_time))
>=8 and extract(hour from convert_timezone('EST', ${TABLE}.call_start_time)) <20
It looks like you want to test if the call_start_time is after 7:30pm EST on ANY day, right?
If so I think you just want to create an interval between the call_start_time and the beginning of that day - call_start_time - date_trunc('day', call_start_time). (Or if you like trunc(timestamp) will give you the date.) Then compare this to the time of day value you want - interval '13 hours 30 minutes'. (13 hours is 19:00 minus 6 hour time zone difference.)
Have I read you question / intent correctly?
====================================
It looks like there is a need to provide a code example. The simplest way to share this is with a sqlfiddle but that means that the syntax will be Postgresql not native Redshift. For the parts of the SQL in question this isn't a change - working with timezones is different but that is handled by an area not in the "interval" calculations.
With prep as (
select *, (t at time zone 'UTC') at time zone 'EST' as est_t,
date_trunc('day', (t at time zone 'UTC') at time zone 'EST') as est_t_date
from fred)
select n, t from prep
where extract(dow from (t at time zone 'UTC') at time zone 'EST') in (6)
and est_t between est_t_date + interval '8 hours'
and est_t_date + interval '19 hours 30 minutes'
;
Live code example at http://sqlfiddle.com/#!17/5236b/2

PostgreSQL What's the difference between NOW() - INTERVAL and its value

I am working with PostgreSQL 12.6 and TimescaleDB.
I have an hypertable raws which contains a field "time" of type timestamp without time zone NOT NULL.
Now I have 2 queries:
select * from raws where time > NOW() - INTERVAL '15 day';
This one gives me this:
2021-04-08 00:00:00
2021-04-08 00:10:00
2021-04-08 00:20:00
And NOW() is equal to: 2021-04-20 16:48:41.575584+02
But now, if I take the value of NOW() - INTERVAL '15 day' which is, at this time 2021-04-05 16:48:41.575584+02, and put it my query like this:
select * from raws where time > '2021-04-05 16:48:41.575584+02';
It doesn't work anymore: I get 0 results, but no errors.
So what is it that makes the second query fail?
Is it something wrong with TimescaleDB or PostgreSQL, or am I missing something with formats?
So I tried to reproduce that bug on a fresh database, and everything worked fine.
The query select * from raws where time > '2021-04-05 16:48:41.575584+02'; stops working when I import all my datas from a dump, so even though I don't know exactly what, there is a problem with that dump.

PostgreSQL: Syntax error at or near "14" when trying to run a delete query

First question here!
So I have a table with a row like this:
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
I would like to run a query that delete all the data in my table older than 14 days.
This is my query:
DELETE FROM customers WHERE timestamp < NOW() - INTERVAL 14 DAY;
This is the error: syntax error at or near "14"
Anyone knows why this is not working and/or how could I achieve my goal??
Many thanks!!
The interval value must be quoted:
DELETE FROM customers WHERE created_at < NOW() - INTERVAL '14 DAYS';
See the doc
DELETE FROM customers WHERE created_at < NOW() - INTERVAL '14 DAY';
Another variation on the existing answers (possibly easier to remember):
DELETE FROM customers WHERE timestamp < NOW() - '14 days'::interval;

Column of type "timestamp(6) with timezone" and "current time" difference in minutes

I have Oracle 12c DB table and one of it's column utc_timestamp is of type
UTC_TIMESTAMP TIMESTAMP(6) WITH TIME ZONE
It stores timestamp in UTC while current_timestamp and systimestamp both gives timestamp in different timezones.
How can I get time difference in MAX(utc_timestamp) and current_timestamp in minutes ignoring time difference due to different time zones.
For example:
select current_timestamp from dual;
Gives=> 23-AUG-17 04.43.16.253931000 PM AMERICA/CHICAGO
select systimestamp from dual;
Gives=> 23-AUG-17 05.43.16.253925000 PM -04:00
select max(UTC_TIMESTAMP) from table_name;
Gives=> 23-AUG-17 09.40.02.000000000 PM +00:00
For above condition when I run SQL to check time difference between in MAX(utc_timestamp) and current_timestamp I should get number 3.
I think I need something like:
select (extract(minute from current_timestamp) - extract(minute from max(UTC_TIMESTAMP)) * 1440) AS minutesBetween from table_name;
But different timezones are messing it up and I get negative number like -4317. This might be correct as current_timestamp will be higher than max(utc_timestamp) being in CST. So I tried:
select (extract(minute from CAST(current_timestamp as TIMESTAMP(6) WITH TIME ZONE)) - extract(minute from max(UTC_TIMESTAMP)) * 1440) AS minutesBetween from table_name;
This SQL runs without error but producing a big negative number like -83461. Please help me find what am I doing wrong.
You really have two problems here.
One is to convert CURRENT_TIMESTAMP to UTC. That is trivial:
select CURRENT_TIMESTAMP AT TIME ZONE 'UTC' from dual [.....]
(use the AT TIME ZONE clause https://docs.oracle.com/cd/B19306_01/server.102/b14225/ch4datetime.htm#i1007699)
The other is that the difference between two timestamps is an interval, not a number.
select current_timestamp at time zone 'UTC'
- to_timestamp_tz('24-AUG-17 04.00.00.000 AM UTC', 'dd-MON-yy hh.mi.ss.ff AM TZR')
from dual;
produces something like
+00 00:02:39.366000
which means + (positive difference) 00 days, 00 hours, 02 minutes, 39.366 seconds.
If you just want the minutes (always rounded down), you may wrap this whole expression within extract( minute from < ...... > ). Be aware though that the answer will still be 2 (minutes) even if the difference is five hours and two minutes. It is probably best to leave the result in interval data type, unless you are 100% sure (or more) that the result is always less than 1 hour.

PostgreSql: how to do where CURRENT_DATE - timestamp > (Value of years/months/days)

May I ask, in PostgreSql, how to do
where CURRENT_DATE - timestamp > (Value of years/months/days).
Here I'd like to calculate the age of a person and verify if it is older or younger than a certain value, so I need to make the subtraction of CURRENT_DATE and timestamp when the person was born, but then I do not know how to do, anyway one could help me?
Thank you very much
You'll need the INTERVAL type.
It can be subtracted from DATE/TIMESTAMP, so your best predicate will be:
WHERE CURRENT_TIMESTAMP - INTERVAL '18 years' > your_timestamp_column
Or, (when the given INTERVAL should be a query parameter, and) if you can only bind basic types (most likely), you can just bind f.ex. an INTEGER year value:
WHERE CURRENT_TIMESTAMP - (? * INTERVAL '1 year') > your_timestamp_column
Technically, your original logic could work too:
WHERE CURRENT_TIMESTAMP - your_timestamp_column > INTERVAL '18 years'
But that won't use any indexes on your_timestamp_column & is subject to INTERVAL justification.