Manipulating Rollup Tables (2)
Data Sample
Below is sample data for ROLLUP test.
create tagdata table TAG (name varchar(20) primary key, time datetime basetime, value double summarized); insert into tag metadata values ('TAG_0001'); insert into tag values('TAG_0001', '2018-01-01 01:00:01 000:000:000', 1); insert into tag values('TAG_0001', '2018-01-01 01:00:02 000:000:000', 2); insert into tag values('TAG_0001', '2018-01-01 01:01:01 000:000:000', 3); insert into tag values('TAG_0001', '2018-01-01 01:01:02 000:000:000', 4); insert into tag values('TAG_0001', '2018-01-01 01:02:01 000:000:000', 5); insert into tag values('TAG_0001', '2018-01-01 01:02:02 000:000:000', 6); insert into tag values('TAG_0001', '2018-01-01 02:00:01 000:000:000', 1); insert into tag values('TAG_0001', '2018-01-01 02:00:02 000:000:000', 2); insert into tag values('TAG_0001', '2018-01-01 02:01:01 000:000:000', 3); insert into tag values('TAG_0001', '2018-01-01 02:01:02 000:000:000', 4); insert into tag values('TAG_0001', '2018-01-01 02:02:01 000:000:000', 5); insert into tag values('TAG_0001', '2018-01-01 02:02:02 000:000:000', 6); insert into tag values('TAG_0001', '2018-01-01 03:00:01 000:000:000', 1); insert into tag values('TAG_0001', '2018-01-01 03:00:02 000:000:000', 2); insert into tag values('TAG_0001', '2018-01-01 03:01:01 000:000:000', 3); insert into tag values('TAG_0001', '2018-01-01 03:01:02 000:000:000', 4); insert into tag values('TAG_0001', '2018-01-01 03:02:01 000:000:000', 5); insert into tag values('TAG_0001', '2018-01-01 03:02:02 000:000:000', 6);
Getting average value using ROLLUP data.
Below is an example of getting the average value in seconds, minutes, and hours for a specific tag.
Mach> SELECT time rollup 1 sec mtime, avg(value) FROM TAG WHERE name = 'TAG_0001' group by mtime order by mtime; mtime avg(value) --------------------------------------------------------------- 2018-01-01 01:00:01 000:000:000 1 2018-01-01 01:00:02 000:000:000 2 2018-01-01 01:01:01 000:000:000 3 2018-01-01 01:01:02 000:000:000 4 2018-01-01 01:02:01 000:000:000 5 2018-01-01 01:02:02 000:000:000 6 2018-01-01 02:00:01 000:000:000 1 2018-01-01 02:00:02 000:000:000 2 2018-01-01 02:01:01 000:000:000 3 2018-01-01 02:01:02 000:000:000 4 2018-01-01 02:02:01 000:000:000 5 2018-01-01 02:02:02 000:000:000 6 2018-01-01 03:00:01 000:000:000 1 2018-01-01 03:00:02 000:000:000 2 2018-01-01 03:01:01 000:000:000 3 2018-01-01 03:01:02 000:000:000 4 2018-01-01 03:02:01 000:000:000 5 2018-01-01 03:02:02 000:000:000 6 [18] row(s) selected. Mach> SELECT time rollup 1 min mtime, avg(value) FROM TAG WHERE name = 'TAG_0001' group by mtime order by mtime; mtime avg(value) --------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 1.5 2018-01-01 01:01:00 000:000:000 3.5 2018-01-01 01:02:00 000:000:000 5.5 2018-01-01 02:00:00 000:000:000 1.5 2018-01-01 02:01:00 000:000:000 3.5 2018-01-01 02:02:00 000:000:000 5.5 2018-01-01 03:00:00 000:000:000 1.5 2018-01-01 03:01:00 000:000:000 3.5 2018-01-01 03:02:00 000:000:000 5.5 [9] row(s) selected. Mach> SELECT time rollup 1 hour mtime, avg(value) FROM TAG WHERE name = 'TAG_0001' group by mtime order by mtime; mtime avg(value) --------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 3.5 2018-01-01 02:00:00 000:000:000 3.5 2018-01-01 03:00:00 000:000:000 3.5 [3] row(s) selected. Mach> SELECT /*+ ROLLUP(TAG, hour, AVG) */ time, value FROM TAG WHERE name = 'TAG_0001' order by time; time value --------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 3.5 2018-01-01 02:00:00 000:000:000 3.5 2018-01-01 03:00:00 000:000:000 3.5 [3] row(s) selected.
Getting min/max values using ROLLUP data.
Below is an example of getting the minimum / maximum value according to the time range of the tag.
Mach> SELECT time rollup 1 hour mtime, min(value), max(value) FROM TAG WHERE name = 'TAG_0001' group by mtime order by mtime; mtime min(value) max(value) -------------------------------------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 1 6 2018-01-01 02:00:00 000:000:000 1 6 2018-01-01 03:00:00 000:000:000 1 6 [3] row(s) selected. Mach> SELECT time rollup 1 min mtime, min(value), max(value) FROM TAG WHERE name = 'TAG_0001' group by mtime order by mtime; mtime min(value) max(value) -------------------------------------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 1 2 2018-01-01 01:01:00 000:000:000 3 4 2018-01-01 01:02:00 000:000:000 5 6 2018-01-01 02:00:00 000:000:000 1 2 2018-01-01 02:01:00 000:000:000 3 4 2018-01-01 02:02:00 000:000:000 5 6 2018-01-01 03:00:00 000:000:000 1 2 2018-01-01 03:01:00 000:000:000 3 4 2018-01-01 03:02:00 000:000:000 5 6 [9] row(s) selected.
Getting Sum/Count values using ROLLUP data.
Below is an example of getting the sum / count value according to the time range of the tag.
Mach> SELECT time rollup 1 min mtime, sum(value), count(value) FROM TAG WHERE name = 'TAG_0001' group by mtime order by mtime; mtime sum(value) count(value) ------------------------------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 3 2 2018-01-01 01:01:00 000:000:000 7 2 2018-01-01 01:02:00 000:000:000 11 2 2018-01-01 02:00:00 000:000:000 3 2 2018-01-01 02:01:00 000:000:000 7 2 2018-01-01 02:02:00 000:000:000 11 2 2018-01-01 03:00:00 000:000:000 3 2 2018-01-01 03:01:00 000:000:000 7 2 2018-01-01 03:02:00 000:000:000 11 2 [9] row(s) selected.
Group at various time intervals
The advantage of the ROLLUP clause is that you don't need to intentionally use DATE_TRUNC () to vary the time interval.
To get the sum of the 3 second intervals and the number of data:
Example The time range is only 0 seconds, 1 second, and 2 seconds, so you can see that all converged to 0 seconds.
As a result, it matches the results of the 'minute rollup' query.
Mach> SELECT time rollup 3 sec mtime, sum(value), count(value) FROM TAG WHERE name = 'TAG_0001' GROUP BY mtime ORDER BY mtime; mtime sum(value) count(value) ------------------------------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 3 2 2018-01-01 01:01:00 000:000:000 7 2 2018-01-01 01:02:00 000:000:000 11 2 2018-01-01 02:00:00 000:000:000 3 2 2018-01-01 02:01:00 000:000:000 7 2 2018-01-01 02:02:00 000:000:000 11 2 2018-01-01 03:00:00 000:000:000 3 2 2018-01-01 03:01:00 000:000:000 7 2 2018-01-01 03:02:00 000:000:000 11 2