What is the "- +" in the redis timeseries commands? - redis

I don't get the examples that have - + in the command. I'm probably missing something that is well-known and I'm just not aware/or know where to get the info but for example at https://redis.io/docs/stack/timeseries/quickstart/
They have command
TS.RANGE sensor1 - + FILTER_BY_TS 1626435230501 1626443276598
but I don't get what the - + is. Does anyone know?

The command format is: TS.RANGE key fromTimestamp toTimestamp
From the doc:
fromTimestamp
is start timestamp for the range query. Use - to express the minimum possible timestamp (0).
toTimestamp
is end timestamp for the range query. Use + to express the maximum possible timestamp.
So in your case, it means DO NOT filter data by timestamp.

TS.RANGE, TS.REVRANGE, TS.MRANGE, and TS.MREVRANGE are range commands. You need to specify the range start and end timestamps.
Instead of specifying concrete values, you can you - and + respectively.
RedisTimeSeries replaces - with the timestamp of the earliest sample in the time series, and + with the timestamp of the latest sample in the time series.
Note that the query you composed
TS.RANGE sensor1 - + FILTER_BY_TS 1626435230501 1626443276598
would report samples only for two specific timestamps: 1626435230501 and 1626443276598, as FILTER_BY_TS is used for specifying a set of exact timestamps - not a range (to retrieve results, you must have samples with these exact timestamps, and these timestamps must fall within [fromTimestamp .. toTimestamp] which is [- .. +] in your case).
If you want to retrieve all samples between timestamp 1626435230501 and timestamp 1626443276598, you should instead use
TS.RANGE sensor1 1626435230501 1626443276598
And if you want to retrieve all samples in the time series, you can use
TS.RANGE sensor1 - +

Related

Why do I get an incompatible value type for my column?

I am trying to calculate the difference between two dates in an oracle database using a JDBC connection. I followed the advice from this question using a query like this:
SELECT CREATE_DATE - CLOSED
FROM TRANSACTIONS;
and I get the following error:
Incompatible value type specified for
column:CREATE_DATE-CLOSED. Column Type = 11 and Value Type =
8.[10176] Error Code: 10176
What should I change so I can successfully calculate the difference between the dates?
note: CREATE_DATE and CLOSED both have TIMESTAMP type
The answer you found is related to date datatypes, but you are dealing with timestamps. While substracting two Oracle dates returns a number, substracting timestamps produces an interval datatype. This is probably not what you want, and, apparently, your driver does not properly handle this datatype.
For this use case one solution is to cast the timestamps to dates before substracting them:
select cast(create_date as date) - cast(closed as date) from transactions;
As it was mentioned, it seems that JDBC cannot work with the INTERVAL datatype. What about casting it with the EXTRACT function to the expected output as number? If you want number of seconds between those two timestamps, it would be:
SELECT EXTRACT(SECOND FROM (CREATE_DATE - CLOSED)) FROM TRANSACTIONS;
Here are list of options which might be used instead of SECOND:
https://docs.oracle.com/database/121/SQLRF/functions067.htm#SQLRF00639
When we subtract one date from another Oracle gives us the difference as a number: it's straightforward arithmetic. But when we subtract one timestamp from another - which is what you're doing - the result is an INTERVAL. Older versions of JDBC don't like the INTERVAL datatype (docs) .
Here are a couple of workarounds, depending on what you want to do with the result. The first is to calculate the number of seconds from the interval result. extract second from ... only gives us the numbers of seconds in the interval. This will be fine providing none of your intervals are more than fifty-nine seconds long. Longer intervals require us to extract minute, hour and even days. So that solution would be:
select t.*
, extract (day from (t.closed - t.create_date)) * 84600
+ extract (hour from (t.closed - t.create_date)) * 3600
+ extract (minute from (t.closed - t.create_date)) * 60
+ extract (second from (t.closed - t.create_date)) as no_of_secs
from transactions t
A second solution is to follow the advice in the JDBC mapping guide and turn the interval into a string:
select t.*
, cast ((t.closed - t.create_date) as varchar2(128 char)) as intrvl_str
from transactions t
The format of a string interval is verbose:INTERVAL'+000000001 04:40:59.710000'DAY(9)TO SECOND. This may not be useful in the Java side of the application. But with regex we can turn it into a string which can be converted into a Java 8 Duration object (docs) : PnDTnHnMn.nS.
select t.id
, regexp_replace(cast ((t.closed - t.create_date) as varchar2(128 char))
, 'INTERVAL''\+([0-9]+) ([0-9]{2}):([0-9]{2}):([0-9]{2})\.([0-9]+)''DAY\(9\)TO SECOND'
, 'P\1DT\2H\3M\4.\5S')
as duration
from transactions t
There is a demo on db<>fiddle

teradata SQL convert real to timestamp

I can't understand how to convert this type of real - 42389.520752314813 to timestamp.
I got this data from one source, but I need to convert it to normal timestamp format.
I think you have received wrong input data.
this type of timestamp is only occurring when Destination tool is excel and which has CELL as "Number" type, and during Copy-Paste of timestamp, destination field has Calculated it as Mathematical function. Please re-verify your source. I am sure about this mathematical calculation. please check the below sample of such data. So practically you cannot perform its reverse operation.
Hard to do without knowing what timestamp should be returned.
If the date is sourced from a SQL Server it might be '2016-01-22 12:29:53':
cast( date '1900-01-01' + myCol as timestamp(3))
+ (cast(86400 * (myCol mod 1) as dec(12,6)) * interval '00:00:01.000000' hour to second)
If it's from Excel it's two days earlier and you must start at '1899-12-30'

store duration in teradata

I am getting source data with duration between 2 timestamps as
Duration Start date End date Start station
14h 26min. 2sec. 12/31/2010 23:49 1/1/2011 14:15 10th & U St NW (31111)
how can I import this data ( which is in CSV file ) in Teradata database to store duration in correct data type, so that I can match it properly with the difference between start and end data?
Please help in correct approach here.
Thanks in advance
That's quite tricky.
A pure SQL based solution (without features of your ETL-tool) needs to generate data which can be safely casted.
This will modify your duration into a format which can be passed to to_dsinterval by removing unneccessary characters besides HMS (target column should be defined as INTERVAL HOUR(4) TO SECOND(0))
Cast(to_dsinterval('PT'||Upper(OTranslate(duration, ' in.ec', ''))) AS INTERVAL HOUR(4) TO SECOND(0))
Your input timestamps show single digit day/month, which Teradata doesn't support (don't aks why), the RegEx adds those missing zeroes (when the seconds are missing remove the :ss part of the format):
Cast(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS TIMESTAMP(0) Format 'mm/dd/yyyyBhh:mi:ss')
Finally pass duration & timestamps as VarChars and apply the Casts during Insert.

Qlikview Timestamp formatting upto microseconds?

In qlikview I can get timestamp in milliseconds, by setting timestamp format as :-
SET TimestampFormat='MM/DD/YYYY hh:mm:ss.fff';
I want to know if there is a way to get time stamp in qlikview upto microseconds.
Formula to optain microseconds from TimeField:
((frac(TimeField) * 86400000) - floor(frac(TimeField) * 86400000)) * 1000 as Micro
And I would use this formula for formatting:
Timestamp(TimeField - (Micro/86400000000)) & Num(floor(Micro), '000') as TimeStamp
As far as I can determine from the QlikView help, there is no format specifier for microseconds, only for milliseconds.
If you need to obtain the microsecond value from a time, I quickly threw the below together (it can probably be done a bit neater). Here I assume your input time field is called TimeField. We can obtain the number of milliseconds using:
=((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000
For the sake of simplicity, I will call the above formula MillisecondCount. Then, using this field, we can then calculate the number of microseconds:
=floor(((MillisecondCount)-floor(MillisecondCount))*1000)
Finally, the full formula to obtain microseconds becomes:
floor(((((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000)
-floor(((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000))*1000)
You can then just format this with num() and append it to your time-stamp string.

SELECT average timestamp difference in JPA with Postgres and HSQL

I have a table with two timestamp columns, startTime and stopTime, and I would like to calculate the average difference of these timestamps in my table. I have a solution that works in Postgres and in HSQLDB (which I use for local unit testing) but not both, and I'm having trouble trying to figure out a common solution.
Postgres:
select EXTRACT(EPOCH FROM(avg(m.stopTime - m.startTime))) from Measures m
HSQL:
select avg(FUNC('UNIX_TIMESTAMP', m.stopTime) - FUNC('UNIX_TIMESTAMP', m.startTime) from Measures m
Is there a way to use the same query for both databases? All of the functions I've found seem to only be supported in one database or the other.
I think my main problem is that there isn't a common function to convert a timestamp to seconds in order to perform the calculation. EPOCH is only compatible with Postgres and UNIX_TIMESTAMP is only compatible with HSQL.
The crux of your problem is converting the dates and timestamps down to a number of seconds. Instead of using epoch, I'll use a julian date for the date. I'll convert the julian date to seconds, then add the number of seconds since minight for each timestamp being compared. The following query does not calculate the difference, it simply converts the date to a number that's similar on both platforms .. you'll have to do this once for each date being compared. note: replace "current"timestamp" with m.startTime and m.stopTime respectively.
select
(to_number(to_char(current_timestamp,'J'),'99999999999999999999')*86400/*convert from julian days to julian seconds*/)
+ (to_number(to_char(current_timestamp,'HH'),'99') * 3600) /*Hours to seconds */
+ (to_number(to_char(current_timestamp,'MM'),'99') * 60) /*Minutes to seconds */
+ (to_number(to_char(current_timestamp,'SS'),'99') /*add in the seconds*/
Ugly as sin, I know-- perhaps you can rewrite it easier as function, but as I don't know hsqls full feature set, I'll leave it in this form rather than using a CTE or function.