I have 2 sql queries to calculate the turnover for each semester.
Results from query 1:
{
"LRU": [
"RADOME",
"RADOME",
"ATSU",
"MFC",
"FWC",
"Unspecified",
"AZE",
"ECP",
"CMM",
"ECP"
],
"Client": [
17346,
17512,
7262,
17242,
4001,
17164,
7277,
17334,
8059,
300015
],
"round": [
-33250,
-13358,
-11731,
-10506,
-6005,
-3132,
-2448,
-2369,
-2236,
-2074
]
}
Results from query 2:
{
"LRU": [
"RADOME",
"RADOME",
"ECP",
"PRIM",
"MFC",
"FWC",
"RCI",
"TAV",
"CAL",
"ECP"
],
"Client": [
17223,
17346,
7262,
7956,
594,
4001,
7277,
17260,
347,
8059
],
"round": [
-34276,
-33250,
-11731,
-6273,
-5684,
-4200,
-2723,
-2586,
-2510,
-2236
]
}
I created a javascript function to calculate the average variation between the 2 queries, sort them, and identify the top 10 customers with increased turnover and the bottom 10 customers with decreased turnover.
Here is my function:
var query1 = {{repair_semester1}};
var query2 = {{repair_semester2}};
var 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
var output = [];
for (client in data) {
for (article in data[client]) {
var turnovers = data[client][article];
output.push(formatName(client,article,turnovers));
}
}
function formatName(client, article, turnover, a) {
return("Client: " + client + ", LRU.: " + article
+ ", semester t/o: " + turnovers
+ " " + (turnovers[0] === 0 ?
turnovers[1] :
((turnovers[1]-turnovers[0]) /turnovers[0])*100
));
}
percent.sort(function (a, b) {
return b.percent - a.percent;
});
return("top", percent.slice(0, 10));
return("bottom", percent.slice(-10));
return(output);
Here are some sample results:
{
"output": [
"Client: 347, LRU.: ECP, semester t/o: 0,-2510 -2510",
"Client: 394, LRU.: ATSU, semester t/o: 0,10433 10433",
"Client: 394, LRU.: FCPC, semester t/o: 0,3023 3023",
"Client: 417, LRU.: FWC, semester t/o: 0,17683 17683",
...
]
}
Please help me correct the function to determine the customers with the highest and lowest turnover variations along with their corresponding LRUs. Thank you.