Finding records older than a minute - sql

I have a users table which consists of
some columns like name,age,created_at
created_at contains time in unix timestamps format (epoch).
I basically want to create a query to show all queries that are older than 1 minute.
I tried the following SELECT * FROM users WHERE created_at > NOW() - INTERVAL '1 minutes';
But i got the following error :
operator does not exist: double precesion > timestamp with time zone

Presumably, created_at is actually a Unix timestamp. That means it is measured in seconds, so you can do:
where created_at < extract(epoch from now() - interval '1 minute')

Related

Select data between dates and between times of day

I want to query rows for a given time range and also filter between given times of day.
Example I want to filter for times of day between '9.00 AM' and '10.00 PM' of every date within a given time range.
My table looks like this.
This is my sample code:
SELECT *
FROM public.energy
WHERE time >= date_trunc('month', NOW() - INTERVAL '1 MONTH') AT TIME ZONE 'Asia/Bangkok'
AND time < date_trunc('MINUTE', NOW()- INTERVAL '1 MONTH') AT TIME ZONE 'Asia/Bangkok'
AND name = 'SWU0001'
ORDER BY id DESC;
I already select data between dates that I want, but I want to filter for specific times.
SELECT *
FROM public.energy
WHERE name = 'SWU0001'
AND time >= date_trunc('month' , now() AT TIME ZONE 'Asia/Bangkok' - interval '1 month') AT TIME ZONE 'Asia/Bangkok' -- !
AND time < date_trunc('minute', now() AT TIME ZONE 'Asia/Bangkok' - interval '1 month') AT TIME ZONE 'Asia/Bangkok' -- !
AND (time AT TIME ZONE 'Asia/Bangkok')::time BETWEEN '09:00' AND '22:00' -- !!!
ORDER BY id DESC;
Don't call a timestamptz column "time". The name is misleading, and it's a basic type name.
Also, to work with local time of 'Asia/Bangkok' you need to get the local time for that time zone before applying date_trunc(), and cast the adjusted value back to timestamptz at the end. Basics:
Ignoring time zones altogether in Rails and PostgreSQL

Difference between CURRENT_TIMESTAMP and CURRENT_DATE

I want to get the data from the last 28 days and only include complete days. So what I mean is, when I look at the data today at 10:00 AM, it only includes data from yesterday (the completed day) and 28 days before yesterday.
I am creating a live dashboard with figures like this. So I don't want the numbers to change until the day is finished.
Also, I am willing to understand the difference between CURRENT_DATE and CURRENT_TIMESTAMP
For example, in my code, if I use CURRENT_TIMESTAMP, will I get the data from today 10:00 AM back to 28 days ago 10:00 AM? if not, how can I get data in a way numbers change live according to every time I run the code (the average time that data change in the database is 10 minutes).
My simplified code:
select count(id) from customers
where created_at > CURRENT_DATE - interval '28 days'
Maybe I am using wrong code, can you please give me advice on how to get the date in both formats:
include only complete days(does not include today, until the day is
finished)
include hours, from today morning until 28 days back same
time in the morning.
Assuming created_at is of type timestamptz.
include only complete days(does not include today, until the day is
finished)
Start with now() and use date_trunc():
SELECT count(*)
FROM customers
WHERE created_at < date_trunc('day', now())
AND created_at >= date_trunc('day', now() - interval '28 days');
Or work with CURRENT_DATE ...
WHERE created_at < CURRENT_DATE
AND created_at >= CURRENT_DATE - 28;
The result for both depends on the current timezone setting. The "date" functionally depends on your current time zone. The type timestamp with time zone (timestamptz) does not. But the expression date_trunc('day', now()) introduces the same dependency as the "day" is defined by your current time zone. So you need to define which "days" you mean precisely. Basics:
Ignoring time zones altogether in Rails and PostgreSQL
You can subtract integer values from a date to subtract days:
How do I determine the last day of the previous month using PostgreSQL?
now() is a shorter equivalent of CURRENT_TIMESTAMP. See:
Difference between now() and current_timestamp
count(*) is equivalent to count(id) while id is defined NOT NULL, but a bit faster.
I have different results from query for COUNT('e.id') or COUNT(e.id)
include hours, from today morning until 28 days back same time in the morning.
Simply:
WHERE created_at > now() - interval '28 days'
No dependency on the current time zone.

Selecting rows created up to 30 seconds ago in Postgres

Say I want to find orders made recently. I am trying this statement:
SELECT * FROM orders WHERE time < now() - interval '30 second';
This doe not work (It does not select any row.) although the following works:
SELECT * FROM orders WHERE time < now();
Do I need to use a fixed time variable? Like:
SELECT * FROM orders WHERE time < '2018-07-01 12:00:00' - interval '30 second';
I am guessing that I should not use now() in statements because it can be changing during operations ...
I believe you have the comparison in the wrong direction:
SELECT *
FROM orders
WHERE time > now() - interval '30 second';
This returns values in the last 30 seconds.
This assumes that time is of an appropriate date type for the comparison.

How to compare dates one with time zone and one without time zone

I have to compare the two dates to get the last 10 min records from database,I'm using postgresql,
I have write this query
select *
FROM x_table
WHERE x_time >= (NOW()::TIMESTAMP WITHOUT TIME ZONE - interval '10 minutes');
x_time is timestamp without time zone so that's why i'm converting the other to the same but it won't giving the result.
Query should written the last 1 min records but i think due to time zone issue it is not giving the result.
how can i resolve the issue?
If x_time is timestamp without time zone, you should specify proper time zone for NOW() function.
Try
select *FROM x_table WHERE x_time >= (NOW() at time zone '-04' - interval '10 minutes')

SQL Get all records older than 30 days

Now I've found a lot of similar SO questions including an old one of mine, but what I'm trying to do is get any record older than 30 days but my table field is unix_timestamp. All other examples seem to use DateTime fields or something. Tried some and couldn't get them to work.
This definitely doesn't work below. Also I don't want a date between a between date, I want all records after 30 days from a unix timestamp stored in the database.
I'm trying to prune inactive users.
simple examples.. doesn't work.
SELECT * from profiles WHERE last_login < UNIX_TIMESTAMP(NOW(), INTERVAL 30 DAY)
And tried this
SELECT * from profiles WHERE UNIX_TIMESTAMP(last_login - INTERVAL 30 DAY)
Not too strong at complex date queries. Any help is appreciate.
Try something like:
SELECT * from profiles WHERE to_timestamp(last_login) < NOW() - INTERVAL '30 days'
Quote from the manual:
A single-argument to_timestamp function is also available; it accepts a double precision argument and converts from Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp with time zone. (Integer Unix epochs are implicitly cast to double precision.)
Unless I've missed something, this should be pretty easy:
SELECT * FROM profiles WHERE last_login < NOW() - INTERVAL '30 days';
How about
SELECT * from profiles WHERE last_login < VALUEOFUNIXTIME30DAYSAGO
or
SELECT * from profiles WHERE last_login < (extract(epoch from now())-2592000)
Have a look at this post:
https://dba.stackexchange.com/questions/2796/how-do-i-get-the-current-unix-timestamp-from-postgresql
and this
http://www.epochconverter.com/