I need to query database like - SELECT FROM table_name WHERE created_at = <--BETWEEN TODAY'S START AND END DATETIME-->
Sample data from created_at column is - 2020-09-28 17:02:14
Tried method is as follows but it didn't work,
SELECT FROM users WHERE created_at=CURRENT_TIMESTAMP;
SELECT FROM users WHERE created_at=CURRENT_DATE;
Can any one help me with this query
Is this what you want?
where created_at >= current_date
and created_at < current_date + interval '1 day'
This gives you all rows whose created_at belongs to the current day.
Related
My database table looks like this:
CREATE TABLE record
(
id INT,
status INT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
And I want to create a generic query to get count of record created after 3 hours of interval in last day
For example, I want to know in last 1 day after 3 hours how many records are created.
What I have so far: with a little help from stackoverflow I am able to create a query to calculate the count for a single full day.
SELECT
DATE(created_at) AS day, COUNT(1)
FROM
record
WHERE
created_at >= current_date - 1
GROUP BY
DATE(created_at)
This is telling me in full day like 24 records are created but I want to get how many are made in interval of 3 hours
If you want the count for the last three hours of data:
select count(*)
from record
where created_at >= now() - interval '3 hour';
If you want the last day minus 3 hours, that would be 21 hours:
select count(*)
from record
where created_at >= now() - interval '21 hour';
EDIT:
You want intervals of 3 hours for the last 24 hours. The simplest method is probably generate_series():
select gs.ts, count(r.created_at)
from generate_series(now() - interval '24 hour', now() - interval '3 hour', interval '3 hour') gs(ts) left join
record r
on r.created_at >= gs.ts and
r.created_at < gs.ts + interval '3 hour'
group by gs.ts
order by gs.ts;
Let' say I have a table web.orders with two columns:
user_id (integer) unique identifier of the user
created_at (integer) timestamp in epoch
And I want to know how many returning customers are there in the last 7 days or the previous month.
So first of all I guess I have list all the unique user_id -s from the last 7 days, then search everyone of them in the earlier part of the table. And then summarise the hits.
I examined this two questions on the subject, but had no luck to convert them to work for me:
Find number of repeating visitors in a month - PostgreSQL
PostgreSQL: Identifying return visitors based on date - joins or window functions?
Please anyone have a solution for this?
You could use exists:
select count(distinct o.user_id) no_returning_visitors
from web.orders o
where
created_at >= extract(epoch from date_trunc('month', current_date) - interval '7' day)
and created_at < extract(epoch from date_trunc('month', current_date))
and exists (
select 1
from web.orders o1
where
o1.user_id = o.user_id
and o1created_at < extract(epoch from date_trunc('month', current_date) - interval '7' day)
)
I am new to SQL but am having a (probably obvious) issue. My goal is to show all data in the month before the last dated entry. When executing the code block below to find the date a month ago,
SELECT MAX(created_at) - INTERVAL '1 MONTH' AS date
FROM shopify_view
it returns, '2019-11-27 11:40:06'. Makes sense!
But when I try to get all the date with a date above that value:
SELECT created_at AS date
FROM shopify_view
GROUP BY created_at
HAVING created_at >= MAX(created_at) - INTERVAL '1 MONTH'
ORDER BY created_at
it returns the first date as '2018-04-23 10:57:28'. Does not make sense!
what am I missing? Thank you!!
You should use a subquery
SELECT *
FROM shopify_view
Where created_at >= (SELECT MAX(created_at) - INTERVAL '1 MONTH' AS date
FROM shopify_view)
ORDER BY created_at
I am trying to select a specific time range on a specific days range in SQL postgres. PLease see the code below which gives an error on the '10:00:00'.
The type of data for each columns is :
numeric for "balance",
character varying(255) for "currency",
timestamp without time zone for "created_at" (ex: 2018-03-20 00:00:00).
I tried this link without success.
MySQL select based on daily timestamp range
SELECT SUM(bl.balance) AS balance, bl.currency, bl.created_at
FROM balance_logs bl
WHERE bl.balance_scope = 'system' AND
created_at >= CURRENT_DATE - 2 AND
created_at < CURRENT_DATE AND
created_at BETWEEN '10:00:00' AND '11:00:00'
GROUP BY bl.currency, bl.created_at
ORDER BY created_at DESC
The comparison needs to be as a time:
SELECT SUM(bl.balance) AS balance, bl.currency, bl.created_at
FROM balance_logs bl
WHERE bl.balance_scope = 'system' AND
created_at >= CURRENT_DATE - 2 AND
created_at < CURRENT_DATE AND
created_at::time BETWEEN '10:00:00'::time AND '11:00:00'::time
GROUP BY bl.currency, bl.created_at
ORDER BY created_at DESC;
However, I think it is better to write the WHERE condition as:
extract(hour from created_at) = 10
How do I select rows in the past starting from yesterday in Oracle DB where a field like created_date is a timestamp(6)?
I don't want to compare time, just date.
If you want exactly one day prior to the current time:
select *
from table t
where created_date < sysdate - 1;
If you want times before today:
select *
from table t
where created_date <= trunc(sysdate);
From the Oracle documentation on SELECT :
SELECT * FROM orders
WHERE created_date < TO_DATE('2014-04-28', 'YYYY-MM-DD');
I can pass this date format from my application, worked like a charm.
As you want to compare just date:
select *
from table t
where date(created_date) < DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
you can use cast function to deal with timestamp as date:
SELECT cast(SYSTIMESTAMP(6) as date)
FROM dual;
so you can select rows with "yesterdate" date by:
select ....
where cast(SYSTIMESTAMP(6) as date) like sysdate - 1
note: replace SYSTIMESTAMP(6) with column name which has timestamp type.