/
Creating and Selecting in Rollup Table
Creating and Selecting in Rollup Table
Since using the ROLLUP clause directly performs a rollup table lookup, to use an aggregate function, it has the following characteristics.
- The aggregate function must be called on the column specified with the SUMMARIZED attribute. However, only the five aggregate functions (SUM, COUNT, MIN, MAX, AVG) supported by the rollup table are supported. (For columns that do not have the SUMMARIZED attribute, all aggregate function calls are possible only for PRIMARY KEY columns and METADATA columns.)
- GROUP BY must be done directly with the BASETIME column to be ROLLUP.
- You can use the ROLLUP clause with the same meaning as it is.
- Alternatively, an alias may be attached to the ROLLUP clause, and the alias may be written in GROUP BY.
SELECT time rollup 3 sec mtime, avg(value) FROM TAG GROUP BY time rollup 3 sec mtime; -- or SELECT time rollup 3 sec mtime, avg(value) FROM TAG GROUP BY mtime;
Data Sample
Below is sample data for rollup test.
create tag 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);
For one tag, different values in seconds were input for 3 hours.
Get ROLLUP AVG
Below is the case of getting average of seconds, minutes, hours of tag table.
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.
Get ROLLUP MIN/MAX Value
Below is the case of getting min/max value of seconds, minutes, hours of tag table. The difference between others, you can get minimum value and maximum value at the same time with just one query.
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.
Get ROLLUP SUM/COUNT
Below is the case of getting sum/count value. Also you can get sum value and count value at the same time with just one query.
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.
Get ROLLUP Sum of Squares
Below is the case of getting sum of squares in rollup.
Mach> SELECT time ROLLUP 1 SEC mtime, SUMSQ(value) FROM tag GROUP BY mtime ORDER BY mtime; mtime SUMSQ(value) --------------------------------------------------------------- 2018-01-01 01:00:01 000:000:000 1 2018-01-01 01:00:02 000:000:000 4 2018-01-01 01:01:01 000:000:000 9 2018-01-01 01:01:02 000:000:000 16 2018-01-01 01:02:01 000:000:000 25 2018-01-01 01:02:02 000:000:000 36 2018-01-01 02:00:01 000:000:000 1 2018-01-01 02:00:02 000:000:000 4 2018-01-01 02:01:01 000:000:000 9 2018-01-01 02:01:02 000:000:000 16 2018-01-01 02:02:01 000:000:000 25 2018-01-01 02:02:02 000:000:000 36 2018-01-01 03:00:01 000:000:000 1 2018-01-01 03:00:02 000:000:000 4 2018-01-01 03:01:01 000:000:000 9 2018-01-01 03:01:02 000:000:000 16 2018-01-01 03:02:01 000:000:000 25 2018-01-01 03:02:02 000:000:000 36 [18] row(s) selected. Mach> SELECT time ROLLUP 1 MIN mtime, SUMSQ(value) FROM tag GROUP BY mtime ORDER BY mtime; mtime SUMSQ(value) --------------------------------------------------------------- 2018-01-01 01:00:00 000:000:000 5 2018-01-01 01:01:00 000:000:000 25 2018-01-01 01:02:00 000:000:000 61 2018-01-01 02:00:00 000:000:000 5 2018-01-01 02:01:00 000:000:000 25 2018-01-01 02:02:00 000:000:000 61 2018-01-01 03:00:00 000:000:000 5 2018-01-01 03:01:00 000:000:000 25 2018-01-01 03:02:00 000:000:000 61 [9] row(s) selected.
Grouping at Various Time Intervals
The advantage of the ROLLUP clause is that it is not necessary to intentionally use DATE_TRUNC() to vary the time interval.
To get the sum of the 3-second interval and the number of data, you can do as follows.
Since the example time range is only 0 sec, 1 sec, and 2 sec, it can be seen that they all converge to 0 sec. As a result, it matches the 'rollup by minute' query result.
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
Related content
Tag Table
Tag Table
Read with this
Meta Table
Meta Table
Read with this
Virtual Table
Virtual Table
Read with this
An example of tag table
An example of tag table
Read with this
롤업 테이블의 생성 및 조회
롤업 테이블의 생성 및 조회
Read with this
Meta Table
Meta Table
Read with this