如果我使用的是Oracle,这个问题可以在几分钟内使用排名功能来解决,但是,不是orientdb所具有的,或者是在其路线图中。
我目前有一个要求,要按分钟级别的时间间隔对属性进行平均和组数据。
花费15分钟的时间跨度,每分钟给出15条记录/ 1条记录,我需要能够以5分钟的间隔对数据进行分组
我的最终结果应该是
平均(最后)............. | ..... GroupedMinutes
0.00103445 ....... | ...... 5
0.00103441 ....... | ...... 10
0.0010344 ......... | ...... 15
示例数据
create class times
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049611',7,29,19,0)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049634',7,29,19,1)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049654',7,29,19,2)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049660',7,29,19,3)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049655',7,29,19,4)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049654',7,29,19,5)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049677',7,29,19,6)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049654',7,29,19,7)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049698',7,29,19,8)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049687',7,29,19,9)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049633',7,29,19,10)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049622',7,29,19,11)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049643',7,29,19,12)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049634',7,29,19,13)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049657',7,29,19,14)
insert into times (exe, Last, month, day, hour, minute) values ('EXE','0.1049698',7,29,19,15)
答案 0 :(得分:4)
我很快写了一个可以帮助你入门的js函数,请记住它没有优化,因为它不能计算不同日期之间的平均值:
var g = orient.getGraph();
var query = g.command("SQL", "SELECT * FROM times ORDER BY month, day, hour, minute");
var avg = 0, group = 0;
var prev_month, prev_day, prev_hour, prev_minute;
var record = [];
for(var i = 0; i < query.length; i++)
{
var month = query[i].getRecord().field("month").toString();
var day = query[i].getRecord().field("day").toString();
var hour = query[i].getRecord().field("hour").toString();
var minute = parseInt(query[i].getRecord().field("minute").toString());
if((minute == (prev_minute + 1) && day == prev_day && hour == prev_hour && month == prev_month) || i == 0)
{
avg += parseFloat(query[i].getRecord().field("Last").toString());
group++;
if(group >= 5 || group >= (query.length-i)+2)
{
record.push((avg / group).toString().substring(0,11));
avg = 0;
group = 0;
}
}
else
{
record.push((avg / group).toString().substring(0,11));
avg = 0;
group = 0;
avg += parseFloat(query[i].getRecord().field("Last").toString());
group++;
if(group >= (query.length-i)+2)
{
record.push((avg / group).toString().substring(0,11));
avg = 0;
group = 0;
}
}
prev_month = month;
prev_day = day;
prev_hour = hour;
prev_minute = minute;
}
return record;