SQL与JavaScript中相交的2个时间序列
我想比较一下在SQL中执行“时间序列交叉”与在JavaScript前端执行“时间序列交叉”的可行性 假设我在这两种情况下都有一个出发点:SQL与JavaScript中相交的2个时间序列,javascript,Javascript,我想比较一下在SQL中执行“时间序列交叉”与在JavaScript前端执行“时间序列交叉”的可行性 假设我在这两种情况下都有一个出发点: //TimeSeries X id, year, value A, 2014, 5 B, 2014, 6 //TimeSeries Y id, year, value B, 2014, 334 若要对此进行散点绘制,我需要构建一个新表,该表包含每个实体和年份的x&y值。在这种情况下,生成的表只有一行 id, year, x, y B, 2014, 6, 3
//TimeSeries X
id, year, value
A, 2014, 5
B, 2014, 6
//TimeSeries Y
id, year, value
B, 2014, 334
若要对此进行散点绘制,我需要构建一个新表,该表包含每个实体和年份的x&y值。在这种情况下,生成的表只有一行
id, year, x, y
B, 2014, 6, 334
1)实现这一点的SQL语法是什么?
2)什么是JavaScript等价物,例如使用Lodash?应该使用简单的连接来执行
Select table1.year, table1.value as x, table2.value as y from table1 join table2 on table1.year=table2.year;
我不推荐javascript实现。Javascript不应用于长时间运行的操作,尤其是服务器端操作
但在我看来,在javascript中实现它的首选方法可以是:
function intersection(xs, ys) {
xs.sort((e1, e2) => e1.year - e2.year);
ys.sort((e1, e2) => e1.year - e2.year);
let output = []
/*
Notice that this function has side effects as it removes elements from the input arrays
if you want to preserve the input arrays, try cloning them instead
*/
while (xs.length > 0 && ys.length > 0) {
if (xs[0].year == ys[0].year) {
// if the two first elements have matching years, add element to output
// notice the use of shift which remove the elements from the beginning of the arrays
output.push({year: xs[0].year, x: xs.shift().value, y: ys.shift().value})
} else if (xs[0].year < ys[0].year) {
// if xs' year is smaller, peel the x
xs.splice(0, 1);
} else {
// else peel the y value
ys.splice(0, 1);
}
}
return output;
}
函数交叉点(xs,ys){
xs.sort((e1,e2)=>e1.year-e2.year);
ys.sort((e1,e2)=>e1.year-e2.year);
让输出=[]
/*
请注意,此函数在从输入数组中删除元素时有副作用
如果要保留输入数组,请尝试克隆它们
*/
而(xs.length>0&&ys.length>0){
if(xs[0].year==ys[0].year){
//如果前两个元素具有匹配的年份,则将元素添加到输出中
//注意shift的用法,它从数组的开头删除元素
push({year:xs[0].year,x:xs.shift().value,y:ys.shift().value})
}else if(xs[0].年
应该使用简单的连接来执行
Select table1.year, table1.value as x, table2.value as y from table1 join table2 on table1.year=table2.year;
我不推荐javascript实现。Javascript不应用于长时间运行的操作,尤其是服务器端操作
但在我看来,在javascript中实现它的首选方法可以是:
function intersection(xs, ys) {
xs.sort((e1, e2) => e1.year - e2.year);
ys.sort((e1, e2) => e1.year - e2.year);
let output = []
/*
Notice that this function has side effects as it removes elements from the input arrays
if you want to preserve the input arrays, try cloning them instead
*/
while (xs.length > 0 && ys.length > 0) {
if (xs[0].year == ys[0].year) {
// if the two first elements have matching years, add element to output
// notice the use of shift which remove the elements from the beginning of the arrays
output.push({year: xs[0].year, x: xs.shift().value, y: ys.shift().value})
} else if (xs[0].year < ys[0].year) {
// if xs' year is smaller, peel the x
xs.splice(0, 1);
} else {
// else peel the y value
ys.splice(0, 1);
}
}
return output;
}
函数交叉点(xs,ys){
xs.sort((e1,e2)=>e1.year-e2.year);
ys.sort((e1,e2)=>e1.year-e2.year);
让输出=[]
/*
请注意,此函数在从输入数组中删除元素时有副作用
如果要保留输入数组,请尝试克隆它们
*/
而(xs.length>0&&ys.length>0){
if(xs[0].year==ys[0].year){
//如果前两个元素具有匹配的年份,则将元素添加到输出中
//注意shift的用法,它从数组的开头删除元素
push({year:xs[0].year,x:xs.shift().value,y:ys.shift().value})
}else if(xs[0].年
谢谢。SQL示例不应该在年份和id上都加入吗?对于JavaScript示例,如果排序后的第一个元素不是同一年,该怎么办?如果x有2013201420152016,y有2011,2013,这真的很稳健吗?让我们举个例子:x有2013201420152016,y有2011,2013。将会发生的是:1。2011年是第二年。2013从x和y 3上剥离。它在y变成空银行后终止。SQL示例不应该在年份和id上都加入吗?对于JavaScript示例,如果排序后的第一个元素不是同一年,该怎么办?如果x有2013201420152016,y有2011,2013,这真的很稳健吗?让我们举个例子:x有2013201420152016,y有2011,2013。将会发生的是:1。2011年是第二年。2013从x和y 3上剥离。它在y变为空后终止