Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm still new with regards on the scripting, and I am cracking this query for quite sometime now. I would want to get the last 2 hour data from the big query. we managed to come up on getting the 5 minute records but I'll appreciate if you could help me getting the 2 hour data. Morelikely we would want to get a data from specific hours. For example 8am-10pm data will be shown during 10:30 am. Appreciate if you could help me on this.

Below is our query getting the 5 minute data

What I have tried:

Select  A.ID, A.Timestamp,  B.Package_quantity,
safe_add(A.Package_quantity, B.Package_quantity) as defect,
from
`tbl_ER020` A
INNER JOIN `tbl_ER220` B
ON A.ID = B.ID
where datetime_add('1900-01-01 00:00:00', interval 27900 + safe_cast(60 * 5 * floor(datetime_diff(current_datetime(),'1900-01-01 00:00:00',minute)/5) as int64) - 450 second) <= datetime(A.Timestamp)
            And DateTime(A.Timestamp) < datetime_add('1900-01-01 00:00:00', interval 27900 + safe_cast(60 * 5 * floor(datetime_diff(current_datetime(),'1900-01-01 00:00:00',minute)/5) as int64) - 150 second)
            ORDER BY A.Timestamp ASC
Posted
Updated 28-Feb-20 5:11am
Comments
Maciej Los 28-Feb-20 2:01am    
What database provider? Looks like MySql or Postgres.

Even if I don't practice SQL, I have enough experience to see a very bad design, something to avoid at all cost.
- Because you use current_datetime() 2 times, you can be unfortunate and have them on a different minute, hour or day which will silently make the query on a different lapse than expected.
- If the SQL compiler is unable to understand that you want the 2 boundaries as constant (which is not said) in the query, it will issue 2 current_datetime() per record because current_datetime() is volatile. Not good either.
- If the SQL compiler is unable to remove the DateTime() from DateTime(A.Timestamp), the server will be unable to leverage an index.

The correct way to do this is:
SQL
-- declare the 2 variables with same datatype as A.Timestamp
declare @start datetime;
declare @End datetime;
declare @DT datetime = current_datetime(); -- read current_datetime() once
-- set start and End values
set @Start = @DT - ... ; -- cancel seconds, minutes and hours as needed
set @End = @Start + ... ; -- add offset as needed

Select  A.ID, A.Timestamp,  B.Package_quantity,
safe_add(A.Package_quantity, B.Package_quantity) as defect,
from
`tbl_ER020` A
INNER JOIN `tbl_ER220` B
ON A.ID = B.ID
where @Start <= A.Timestamp And A.Timestamp < @End
ORDER BY A.Timestamp ASC;

Why doing the query this way?
Your query runtime depend on the number of records of datatable.
My query runtime depend on the number of records of select result.
On a huge datatable, it makes a big difference.
[Update]
Read there: PostgreSQL: Documentation: 9.1: Function Volatility Categories[^]
Quote:
A VOLATILE function can do anything, including modifying the database. It can return different results on successive calls with the same arguments. The optimizer makes no assumptions about the behavior of such functions. A query using a volatile function will re-evaluate the function at every row where its value is needed.
 
Share this answer
 
v7
Comments
ZurdoDev 28-Feb-20 15:13pm    
"2 current_datetime() per record" - I don't think that part is right. Sql treats statements as a set and so all records in a set will get the same time even using current_datetime()
ZurdoDev 28-Feb-20 16:26pm    
That's for postgre sql. I was referring to Ms Sql. I couldn't tell from OP what version it was. And I'm still not convinced it means what you think it means. It might though.

For example,
SELECT t.*, current_datetime()
FROM anytable t

Does the last column change? Or do all records in the result set have the same time stamp?
Patrice T 28-Feb-20 17:03pm    
Many SQL servers, many possibilities.
You just have to run the query on a big database to get the answer.
Just make sure the query runs a few seconds.
My solution is a slight refinement to the above

declare @d1 datetime = dateadd(hh,-2, getdate()) 
declare @d2 datetime = getdate()
Select  A.ID, A.Timestamp,  B.Package_quantity,
safe_add(A.Package_quantity, B.Package_quantity) as defect,
from
`tbl_ER020` A
INNER JOIN `tbl_ER220` B
ON A.ID = B.ID
where a. timestamp between @d1 and @d2
            
            ORDER BY A.Timestamp ASC


Date Add is covered her

DATEADD (Transact-SQL) - SQL Server | Microsoft Docs[^]


Between is here

BETWEEN (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900