InfluxDB: Query to select data from two time ranges at the same time - where-clause

I am using InfluxDB for tick data from Tokyo Stock Exchange, where trading happens between 09:30 and 11:30 (AM session) and then from 12:30 to 15:00 (PM session), with a one hour lunch break from 11:30 to 12:30. I am interested in analyzing data from the both sessions, but not from the lunch break.
The following query works and gives me results
SELECT COUNT(<fieldname>) FROM <measurementname> where
(time >= '2017-11-01 09:00:00' and time < '2017-11-01 11:30:00') and
<other_conditions>
The following query also works and gives me results
SELECT COUNT(<fieldname>) FROM <measurementname> where
(time >= '2017-11-01 12:30:00' and time < '2017-11-01 15:00:00')
and <other_conditions>
But if I try the following, it does not work: I get an empty result with a warning (no series returned)
SELECT COUNT(<fieldname>) FROM <measurementname> where
((time >= '2017-11-01 09:00:00' and time < '2017-11-01 11:30:00') or (time >= '2017-11-01 12:30:00' and time < '2017-11-01 15:00:00'))
and <other_conditions>
How can I combine the two time conditions for AM and PM session into one query?

For recent InfluxDB versions 2.X, it now uses Flux for querying and there is a solution for such problems here:
https://community.influxdata.com/t/query-specific-time-range-and-day/14503

Related

Within "Where" Clause filter the same time period between two different days

To clarify the title:
I've got two columns:
VisitationDate: The date that someone visited a store. ex.) '2020-01-01'
VisitationDateTime: A DateTime object. ex.) '2020-01-01 00:00:00'
Within a where clause, I'm trying to select/filter a date range that is between '2020-01-01 00:00:00' to '2020-01-01 12:00:00' and the same exact time frame but on 01/03.
Other way of writing it:
I want the select these date/time ranges:
2020-01-01: Midnight to 12PM
2020-01-03: Midnight to 12PM
select * from myTable
where (visitationDateTime >= '20200101' and visitationDateTime < '20200101 12:00') or
(visitationDateTime >= '20200103' and visitationDateTime < '20200103 12:00');
Note that using BETWEEN for datetime range checks in MS SQL server is not a good idea. The better way is to use upper boundary exclusive using <.

Presto SQL / Athena: select between times across different days

I have a database that contains a series of events and their timestamp.
I find myself needing to select all events that happen between 11:00 and 11:10 and 21:00 and 21:05, for all days.
So what I would do is I extract from timestamp the hour and the minute, and:
SELECT *
WHERE (hour = 11 AND minute <= 10)
OR (hour = 21 AND minute <= 05)
However, I was wondering if there's a simpler / less verbose way to do this, such as when you query between dates:
SELECT *
WHERE date BETWEEN '2020-07-01' AND '2020-07-05'
I read here that this is doable in SQLite, I was wondering if it's possible to be done in presto as well. I've looked at the docs but couldn't find an analogue function that does what time() does in SQLite.
You could use date formatting functions, e.g. date_format, then string comparisons:
select *
from mytable
where
date_format(mydate, '%H:%i') between '11:00' and '11:09'
or date_format(mydate, '%H:%i') between '21:00' and '21:04'
Note that I substracted one minute from the upper bound, since I assume you don't want to include the last minute. between '11:00' and '11:09' gives you everything from 11:00:00 to 11:09:59.

Rounding DateTime in SQL

I have DateTime data in a MS SQL database with the following format:
2020-05-07 22:35:00
I am trying to create a query that only captures data from the last 24 hours of operations. However, our operations KPIs are measured from 6AM-6AM. I would like to round the date based on time. Anything before 6AM will be counted as the day before.
2020-05-07 05:45:00 -> 2020-05-06 (Before 6AM)
2020-05-07 06:30:00 -> 2020-05-07 (After 6AM)
So far I have been successful in pulling the previous days activity, but am struggling to shift the timeframe to round down anything before 6AM
SELECT
end_date
FROM data sint
WHERE sint.end_date >= dateadd(day,datediff(day,1,GETDATE()),0)
AND sint.end_date < dateadd(day,datediff(day,0,GETDATE()),0)
You can add six hours to the current date (without the time) for the comparison:
where int.end_date < dateadd(hour, 6, convert(datetime, convert(date, getdate()))) and
int.end_date >= dateadd(hour, 6 - 24, convert(datetime, convert(date, getdate())))
Note that the conversion to date removes the time component.
first get the time part. Compare the time part with 6AM and run your expression.
In below code the time part is compared with the date '1900-01-01 06:00:00' which is 6AM in default date format.
select
case when cast(date as time(0)) < '1900-01-01 06:00:00'
then cast(date - 1 as date)
else cast(date as date)
end as newdate
from temp_date;
I would do the same as Gordon only the opposite side.
I would subtract 6 hours from end_date and then compare.
where convert(date, dateadd(hour, -6, int.end_date)) = convert(date, getdate())
Though this method might not work well if you're actually using getdate(). It will return nothing if it is ran between midnight and 6 am (unless you have future dates).

How to change both date and time in a datetime in SQL

I try to set the time and date in my query based one the following conditions:
if time in MyDate < 9:00 then set the time to 9:00
If time in 9:00 < MyDate < 15:00 then set the time to 16:00
If time is MyDate > 15:00 then set the time to 9:00 and the day to day+1
I have the two first conditions in place and works fine, but cannot combine changes in both time and date. How can I do that?
The code below works fine for the two first conditions!
Case When cast(MyDate as TIME) < '09:00:00' Then DATEADD(Hour, 9, CAST(CAST(PayoutDtApplication As Date) As Datetime))
Case When cast(MyDate as TIME) < '09:00:00' Then DATEADD(Hour, 9, CAST(CAST(PayoutDtApplication As Date) As Datetime))
THanks
This is a guess, based on the following comment:
this numbers can varries based on the data I get, just wanted to say I need to set the time to 9:00 and day+1 for all dates I get. I have different dates.
I'm guessing that regardless of the time, the OP wants to change the value to the following date at 09:00:00.
If so, one way to achieve it would be:
SELECT DATEADD(HOUR, 33, CONVERT(date,YourDateColumn)) AS NewDate
FROM YourTable;
Again, this is guesswork. If the OP elaborates, I'll be happy to expand my answer, or remove it if it's irrelevant.
Edit: Based on the OP's new logic from their edit:
CREATE TABLE #Sample (YourDate datetime2(0));
INSERT INTO #Sample
VALUES ('2018-05-09T08:57:00'),
('2018-05-09T14:26:37'),
('2018-05-09T19:24:01');
GO
SELECT YourDate,
CASE WHEN CONVERT(time, YourDate) < '09:00:00' THEN DATEADD(HOUR,9,CONVERT(datetime2(0),CONVERT(date,YourDate)))
WHEN CONVERT(time, YourDate) > '15:00:00' THEN DATEADD(HOUR,33,CONVERT(datetime2(0),CONVERT(date,YourDate)))
ELSE DATEADD(HOUR,15,CONVERT(datetime2(0),CONVERT(date,YourDate))) END AS NewDate
FROM #Sample;
GO
DROP TABLE #Sample;
The double CONVERT (CONVERT(datetime2(0),CONVERT(date...) is because the data type date isn't compatible with DATEADD(HOUR.... I have used datetime2 as this should be used over datetime now.

SQL Find if two UTC Dates are in the same calendar day

I have a table with a DateTime column, this column contains a DateTime in UTC which in local time is always time 00:00:00.000, this is because I only care about Date part, not time but I need to store the Date in UTC.In my local time, the UTC offset is -6 hours, so, all my dates are stored with a time equals to 6, for example, '2017-01-03 06:00:00.000' . The problem is that when I try to compare this:
SELECT (CASE WHEN (CONVERT(DATE, '2017-01-03 06:00:00.000') = CONVERT(DATE, GETUTCDATE())) THEN 1 ELSE 0 END) AS [IsEqual]
If the result of GETUTCDATE is another day but the hour is smaller than 6, for example, '2017-01-04 02:00:00.000', this date in local time still belong to January 3, but the above comparison returns false because is checking if 2017-01-03 = 2017-01-04.
How can I find id this two UTC dates are on the same calendar day?
If your local time is always 6 hours from UTC (meaning no Daylight saving time), you should subtract 6 hours from GETUTCDATE() and then compare to your local time:
SELECT CASE WHEN
CONVERT(DATE, '2017-01-03 06:00:00.000') =
CONVERT(DATE, DATEADD(HOUR, -6, GETUTCDATE())) THEN
1
ELSE
0
END AS [IsEqual]