Picking order details from database in an interval - sql

I want to pull orders that have been placed in between (last 5 mins - last 10 mins). For that I am running a query:
$query="SELECT * FROM orders where `orderStatus`='PARTIAL' and `timeCreated` >= date_sub(now(),interval 10 minute)";
However, this query is picking data from between (last 0 mins - last 10 mins).
Can you please help what should I try?

Just add another filtering condition:
SELECT *
FROM orders
WHERE
orderStatus = 'PARTIAL'
AND timeCreated >= date_sub(now(), interval 10 minute)
AND timeCreated < date_sub(now(), interval 5 minute);
Assuming that you are using MySQL (the syntax of your existing query suggests it), you can also phrase the query using the following date arithmetics, which I find more straight-forward:
SELECT *
FROM orders
WHERE
orderStatus = 'PARTIAL'
AND timeCreated >= now() - interval 10 minute
AND timeCreated < now() - interval 5 minute

Related

Take last 3 hours and group by 5 minutes [duplicate]

This question already has answers here:
Postgresql SQL GROUP BY time interval with arbitrary accuracy (down to milli seconds)
(10 answers)
Closed 1 year ago.
I was wondering if you can help me write a query that should just SELECT count(*) but only include data from last 3 hours and group it by 5 minutes.
So I have a table that has a createdts so I have the date there. I just want to see how many entries I have in the last 3 hours, but group COUNT(*) per 5 minutes.
SELECT COUNT(*)
FROM mytable
WHERE createdts >= now()::date - interval '3 hour'
GROUP BY 'every 5 minutes'
Also, what's really important is that the Count(*)'s that are null, get defaulted to 0. I have many windows of time where the data will be null, and having it default to 0 saves a lot of headache later
Use generate_series():
SELECT gs.t, COUNT(t.createdts)
FROM GENERATE_SERIES(now()::date - interval '3 hour', now()::date, interval '5 minute') gs(t)
mytable t
ON t.createdts >= gs.t AND
t.createdts < gs.t + interval '5 minute'
GROUP BY gs.t;

Get daily count of rows for a Time

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;

select 10 day old entries BigQuery

I need to get only entries that are 10 days + old in BigQuery, I went to other questions and google doc (which is confuse as hell) but could not find a the result I expected.
My query:
SELECT Id
FROM myTable
WHERE eventTS > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)
The this query is returning values that are not 10 days + old.
when I add -10 DAY doesn't return nothing.
Any help is MUCH welcome!
Thanks in advance.
Try following Bigquery functions SELECT CURRENT_TIMESTAMP() as Curr_Ts, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY) as Sub_Ts , TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -10 DAY) as Add_Ts It will Add & Substract 10 days from CURRENT_TIMESTAMP(). Output will be as :-
Your Query will get modified as :-
SELECT Id
FROM myTable
WHERE eventTS < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)
Or
SELECT Id
FROM myTable
WHERE eventTS < TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -10 DAY)
Try replacing > with <:
SELECT Id
FROM myTable
WHERE eventTS < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)

Querying last 5 years

I want to query all products sold in the last 5 years.
It is possible to do it like this:
select * from products
where time between sysdate-1826 and sysdate
But it there also a nicer way instead of calculating all the days and subtract it from sysdate?
SELECT *
FROM products
WHERE date_column >= add_months( sysdate, -12*5 )
or
SELECT *
FROM products
WHERE date_column >= sysdate - interval '5' year
will both give you all the rows from the last 5 years (though you would generally want to add a TRUNC to remove the time portion unless you really care whether a row was created on Feb 8, 2007 in the morning or in the afternoon).
select * from products
where time > DATE_SUB(NOW(), INTERVAL 5 YEAR)
Date sub will subtract 5 years from now

Best way to get recent date rows? (this week/month)

I wanna get rows in recent date, this week or month, etc. suppose the table has a field named: product_date.
To get the rows in the last month, you could use something like:
SELECT * FROM table WHERE product_date >= DATE_SUB(NOW(), INTERVAL 1 MONTH);
Or for the last week:
SELECT * FROM table WHERE product_date >= DATE_SUB(NOW(), INTERVAL 1 WEEK);
For within the last seven days:
WHERE product_date BETWEEN DATE_ADD(NOW(), INTERVAL -7 DAY) AND NOW()
For the last month:
WHERE product_date BETWEEN DATE_ADD(NOW(), INTERVAL -1 MONTH) AND NOW()
While mopoke's solution gets results from last 30 or 7 days, this gives you results for current month only:
SELECT * FROM table WHERE DATE_FORMAT(product_date, '%c-%Y') = DATE_FORMAT(CURDATE(), '%c-%Y');
Or for current week:
SELECT * FROM table WHERE DATE_FORMAT(product_date, '%u-%Y') = DATE_FORMAT(CURDATE(), '%u-%Y');