如何在同一循环中比较两个查询结果

时间:2017-08-17 08:29:52

标签: javascript

我有2 sql queries(每个学期一个学期)。这两个查询的行数不同。每个参数包含3个参数:Article id_Customer Turnover

这是在计算第一学期的请求之后:

select "LRU", "Client", round(sum("Montant_fac_eur"))
from "foundry_sync"."data"
where "Nature"='Repair'     

and extract(month from "Facturation") between 0+{{w_widget6.selectedValue}} and 5+{{w_widget6.selectedValue}}
group by "LRU", "Client"

查询的学期代表如下:

{
  "LRU": [
    "ATSU",
    "ATSU",
    "ATSU",
    "ATSU
      . 
      .
      .
   ],
"Client": [
    394,
    594,
    4001,
    5725,
    7057,
    7090
    .
    .
    .
],
 "round": [
    20866,
    16814,
    27421,
    0,
    6593,
    66965,
    8401
     . 
     .
     .
]

以下是结果样本:

   result 1                      result2

"[Article1,394,3914]"      "[Article1,394,3914]"
"[Article1,594,16814]"     "[Article2,594,10088]"
"[Article1,4001,26798]"    "[Article1,4001,11107]"
"[Article3,5725,0]"        "[Article3,5725,0]"
"[Article5,7057,5700]"     "[Article5,7057,3916]"
"[Article8,7090,56467]"    "[Article4,7090,17558]"
"[Article2,7236,8401]"     "[Article8,7236,8401]"
"[Article11,7242,12766]"   "[Article14,7242,4255]"
"[Article2,7262,892]"      "[Article2,7262,892]"
"[Article3,7302,17234]"    "[Article3,7302,8928]"

如您所见,客户可以在两个学期内完成一篇文章的更替,或者只需一个学期(第一个或第二个学期)的文章翻译。 我想首先比较每个学期每Articlecustomer的营业额。

    var query1 = {{repair_semestre1}};
    var query2 = {{repair_semestre2}};
    var result1 = [];
    var result2 = [];


    for (var i = 0; i < query1.LRU.length; i++) {
      result1.push(formatName(query1.LRU[i], query1.Client[i], query1.round[i]));  
    }

    for (var i = 0; i < query2.LRU.length; i++) {
      result2.push(formatName(query2.LRU[i], query2.Client[i], query2.round[i]));  
    }

    return {
      result1: result1, 
      result2: result2
    };

   /*   for (var i = 0; i < query1.LRU.length, i < query2.LRU.length; i++) {
        if((query1.LRU[i] == query2.LRU[i]) && (query1.Client[i] == query2.Client[i])) {
                    if(query1.round[i] > query2.round[i]) {
                            return "ok";
                    }
                    else { return "non";
                    }
        }

    }  */

    function formatName(lru, turnover, round) {
      return "[" + lru + "," + turnover + "," + round + "]";
    }

我在函数外面为这两个查询做了一个循环来比较她的结果:

var query1 = {{repair_semestre1}};
var query2 = {{repair_semestre2}};
for (var i = 0; i < query1.LRU.length, i < query2.LRU.length; i++) {
    if((query1.LRU[i] == query2.LRU[i]) && (query1.Client[i] == query2.Client[i])) {
                if(query1.round[i] > query2.round[i]) {
                        return "ok";
                }
                else { return "not ok";
                }
    }   
}  

它仅返回not ok,并且不会完成其余的行。可以请一位,解释一下为什么? 如何在我的函数中添加此循环来运行?

谢谢。

1 个答案:

答案 0 :(得分:1)

我建议首先将您的数据结构转换为在一个对象中将相关事物组合在一起的内容,而不是使用索引号的链接。

例如,对于涉及两篇文章的客户594,学期转换可以表示如下:

{
  "594": {
    "Article1": [
      16814,
      0
    ],
    "Article2": [
      0,
      10088
    ]
  }
}

...内部数组总是有两个条目:两个学期中的每个学期。该结构将允许轻松报告。

以下是转换的代码,其后面是示例输出:

// Sample data
const query1 = {
    LRU: ["Article1", "Article1", "Article1", "Article3", "Article5", 
          "Article8", "Article2", "Article11", "Article2", "Article3"], 
    Client: [394, 594, 4001, 5725, 7057, 7090, 7236, 7242, 7262, 7302],
    round: [3914, 16814, 26798, 0, 5700, 56467, 8401, 12766, 892, 17234]
}, query2 = {
    LRU: ["Article1", "Article2", "Article1", "Article3", "Article5", 
          "Article4", "Article8", "Article14", "Article2", "Article3"], 
    Client: [394, 594, 4001, 5725, 7057, 7090, 7236, 7242, 7262, 7302],
    round: [3914, 10088, 11107, 0, 3916, 17558, 8401, 4255, 892, 8928]
};

// Convert to a more useful data structure, keyed by client and article
const data = {};
[query1, query2].forEach(function (query, semester) {
    query.Client.forEach(function(clientId, index) {
        var client = data[clientId] = data[clientId] || {};
        var clientArt = client[query.LRU[index]] = client[query.LRU[index]] || [0, 0];
        clientArt[semester] = query.round[index];
    });
});

// Now report on that data
for (const client in data) {
    for (const article in data[client]) {
        const turnovers = data[client][article];
        console.log('Client: ' + client + ', art.: ' + article 
                  + ', semester t/o: ' + turnovers
                  + ' ' + (turnovers[0] === turnovers[1] ? 'equal' : 
                          turnovers[0] < turnovers[1] ? 'increase' : 
                                                        'decrease'));
    }
}
.as-console-wrapper { max-height: 100% !important; top: 0; }