Count by minute in Riak TS - riak

I'm trying to grasp the recently added group by in Riak TS.
I'm unable to find a way to group my results by minute, e.g. count. I'll show an example below.
CREATE TABLE Results
(
result VARCHAR NOT NULL,
time TIMESTAMP NOT NULL,
PRIMARY KEY (
(QUANTUM(time, 1, 'm')),
time
)
)
Inserts
INSERT INTO FreightMinuteResult VALUES ('Novo', '2017-12-07 12:03:45Z');
INSERT INTO FreightMinuteResult VALUES ('Novo', '2017-12-07 12:04:45Z');
INSERT INTO FreightMinuteResult VALUES ('Novo', '2017-12-07 12:05:45Z');
INSERT INTO FreightMinuteResult VALUES ('Novo', '2017-12-07 12:05:46Z');
Query
select count(*) from FreightMinuteResult where time > '2017-12-07 12:01:00Z' and time < '2017-12-07 12:06:00Z' group by time;
The result is
+--------+--------------------+
|COUNT(*)| time |
+--------+--------------------+
| 1 |2017-12-07T12:04:45Z|
| 1 |2017-12-07T12:03:45Z|
| 1 |2017-12-07T12:05:45Z|
| 1 |2017-12-07T12:05:46Z|
+--------+--------------------+
How to count the number of occurrences per minute using Riak TS?
Thanks.

The quantum is used to organize the data in the backend to streamline query operations, while group by uses the exact value of the specified field. The timestamps 2017-12-07T12:05:45Z and 2017-12-07T12:05:46Z occur in the same minute and will therefore be stored in the same location on disk, but they are still stored as distinct second-resolution timestamp values that will be grouped separately.
If you want to be able to group by the minute you will need to either round the timestamps when inserting, or modify your table to include a minute field that can be grouped.

Related

Get maximal value per Azure Data Explorer table from tables with same schema

I have multiple tables with telemetry Metric_1, Metric_2, Metric_3 and all those tables have the same schema (e.g. they contain Timestamp column). I'd like to get the most recent timestamp per table.
I found possibility of using union wildcard, but query
union Metric_*
|summarize Max= max(Timestamp)
never actually finished.
Query
Metric_1
|top 1 by Timestamp
takes no time. But even summarize on a single table takes forever (I killed it after 2 minutes)
Metric_1
|summarize Max= max(Timestamp)
Can you explain the time difference and suggest how to accomplish what I need? The outcome should be
Table | MaxTimestamp
Metric_1 | Date1
Metric_2 | Date2
Metric_3 | Date3

Difference between table row values in the same

I have a SQLite table:
CREATE TABLE `Readings` ( `ID` TEXT, `Reading` TEXT, `Date` TEXT )
Every Date I have real Readings from different sensors identified by IDs. Is it possible to get a result table with differences between Readings from sensors with the same ID but for different Dates?
Assuming that you are using a proper date format, you can look up the corresponding previous value with a correlated subquery:
SELECT ID,
Date,
Reading - (SELECT Reading
FROM Readings AS R2
WHERE R2.ID = Readings.ID
AND R2.Date < Readings.Date
ORDER BY Date DESC
LIMIT 1
) AS Difference
FROM Readings;

How to substitute part of date string with table.field value in sqlite3?

I have the following 2 tables:
CREATE TABLE count (nbr int not null);
and
CREATE TABLE day (day int not null);
Table count has 3 records with field values:
1
2
3
Now I want to insert a calculated date in table day based on the current date and a value from table count with the following statement:
insert into day values (date('now', '+'(select nbr from count where nbr=1) 'day'));
No matter what I change and (re)try in the statement I keep getting 'Syntax error' messages or the message that day.day may not be NULL.
Is it possible to use the select statement in this case anyway (with correct syntax of course) and if so, what am I doing wrong?
Found the solution, it was only a matter of correct concatenation.
This works:
insert into day select (date('now', '+'||(select nbr from count where nbr=1)||' day'));

HP Vertica: partition by TIMESTAMPTZ field

I'm trying to re-partition some table using week number counting from some day:
my_fact table contains a field called time_stamp of type TIMESTAMPTZ
Unfortunately, re-partition doesn't work, and I'm getting the error:
MyDB=> ALTER TABLE my_fact PARTITION BY MOD(TIMESTAMPDIFF('day', time_stamp::TIMESTAMP, TIMESTAMP '2013-09-23'), 156) REORGANIZE;
NOTICE 4954: The new partitioning scheme will produce 12 partitions
ROLLBACK 2552: Cannot use meta function or non-deterministic function in PARTITION BY expression
Should the cast of time_stamp to TIMESTAMP strip any time zone related info from this field thus making it deterministic?
Thanks!
Take a look at the date_part() function, you can use the TIMESTAMPTZ as its source column:
Example :
**The number of the week of the calendar year that the day is in.**
SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 7
SELECT EXTRACT(WEEK FROM DATE '2001-02-16');
Result: 7
Since I got no answer, I'm writing here what I've ended up with:
ALTER TABLE my_fact PARTITION BY
MOD(
TIMESTAMPDIFF(
'day',
'2013-09-23'::timestamptz AT TIME ZONE 'UTC',
time_stamp AT TIME ZONE 'UTC'),
156)
REORGANIZE;
This solution works.

SQLite3 /Python: Update Query to populate a field by taking the difference from two fields in the same table

My table: tblTest:
RunID,StartTime, EndTime, Period
1,2013-03-30 18:08:14-04,2013-04-01 10:57:22-04
2,2013-04-03 12:13:10-04,2013-04-03 18:05:34-04
3,2013-04-04 06:02:30-04,2013-04-05 10:42:00-04
4,2013-04-05 10:43:00-04,2013-04-06 13:23:06-04
I am attempting to update the table to calculate the column Period.
The query that I am using is:
UPDATE tblTest SET Period = (SELECT strftime('%s',substr(endtime,1,19)) -
strftime('%s',substr(starttime,1,19)) From tblTest)
but to my surprise it updates all Periods with the same value from the first line.
What am I doing wrong?
A subquery like (SELECT ... FROM tblTest) without a WHERE condition returns all records of the table.
In a context where only one value is expected (like in the SET expression), only the first such record is used.
You can just directly access the columns of the table that you are updating:
UPDATE tblTest
SET Period = strftime('%s', substr(EndTime, 1, 19)) -
strftime('%s', substr(StartTime, 1, 19))

Resources