Attempting to write queries that will be compatible with both PostgreSQL and Amazon Redshift.
Reason: Syncing data from PG to RS to perform complex queries, but in dev/QA environments budget (and DB size) dictates to stay with PG only.
Request: return yesterday's date
In PostgreSQL:
SELECT DATE((NOW() - '1 DAY'::INTERVAL));
In Redshift:
SELECT DATE(DATEADD(DAY, -1, GETDATE()));
Problem: Neither works in the other platform.
Is there a compatible way to achieve requested action?
ORM is an option we'd like to avoid.
The following works in Postgres and Redshift:
ANSI standard SQL:
SELECT current_date - interval '1' day;
-- 2018-06-19 00:00:00
SELECT current_timestamp - interval '1' day;
-- 2018-06-19 13:40:06.509337+00
Postgres (and I believe Redshift as well) also supports the alternative (non-standard) interval syntax: interval '1 day'
Or more compact (not 100% ANSI SQL but works in both):
SELECT current_date - 1;
-- 2018-06-19 00:00:00
SELECT current_timestamp - 1;
-- 2018-06-19 13:40:06.509337+00
Related
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.
I want to migrate or write an equivalent query to get the data from table one hr before the current time in PostgreSQL.
oracle query:
select *
from T_DATA as of timestamp (systimestamp - interval '60' minute);
select * from T_DATA where timestamp_column >= now() - interval '1 hour'
Since flashback queries are not supported in postgresql, One approach I tried with temporal tables extension.
Is there an easy way to create a replica table on the same postgresql server that is delayed by 24 hours?
I have tried creating a cron that does an insert with a command like this
INSERT INTO TABLE_BACKUP
SELECT * from TABLE_new WHERE time< (NOW() - INTERVAL '1 day');
I have found that doing the above some records are missing.
Is there a better way?
I'm having trouble with using the between operator in where clause. I have the following queries:
SELECT *
FROM table
WHERE timestamp_column BETWEEN (current_date - interval '1 day')::date
AND current_date => 500k rows
SELECT *
FROM table
WHERE timestamp_column >= (current_date - interval '1 day')::date => 1 mil rows
Does anyone have any idea why the result set is different? Shouldn't it be the same? I am trying to compare the number of rows from a database in PostgreSQL with data from Sybase ASE. The same 2 queries ran in Sybase give the same results. The expected result set is 1 milion rows. This should be the number of rows that I have between yesterday and today.
PostgreSQL v9.5, timestamp_column = timestamp without time zone
I tried using now(), current_timestamp but same result.
What am I missing here? If I didn't made myself clear let me know.
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