I'm working on google data studio and trying to fetch the data from a third-party system. In that case, there is a problem in the third-party system, because it doesn't allow for SQL join. Therefore, I got the data by using 2 URLs separately and joined the two tables(customer_order table & Customer_Order_Demand table). But the problem is there is a field(Order_NO) that has many to many relationships in one table. According to my code, I'm getting only first value in Order_No column after joining the two tables. I want to display the number of orders for each Item by corresponding date.
Customer Order Demand Table
+-----------------------------+
| id | item | Date |
|-----|----------|------------|
| 1 | BLU | 7/10/2020 |
| 2 | PM | 7/20/2020 |
| 3 | BLU | 7/23/2020 |
+-----------------------------+
Customer Order Table
+---------------------------------------------------------+
| id | COD_id | Order_NO | Date |
|-----|----------------------------------|----------------|
| 1 | 1 | #100 | 7/10/2020 |
| 2 | 1 | #130 | 7/10/2020 |
| 3 | 2 | #200 | 7/20/2020 |
| 4 | 3 | #500 | 7/23/2020 |
+---------------------------------------------------------+
Expected Table
+-----------------------------------------------+
| | Item | Date | Order_NO |
|-----|----------|------------|-----------------|
| 1 | BLU | 7/10/2020 | #100 /#130 |
| 2 | PM | 7/20/2020 | #200 |
| 3 | BLU | 7/23/2020 | #500 |
+-----------------------------------------------+
var url=[
'http://test.smartapps.lk/rest/api/selectQuery?sessionId=' + SessionId + '&maxRows=10&query=select id, Item, Date from Customer_Order_demand &output=json'
].join('');
var url2=[
'http://test.smartapps.lk/rest/api/selectQuery?sessionId=' + SessionId + '&maxRows=10&query=select id,Order_NO from Customer_Order &output=json'
].join('');
var responseID2 = UrlFetchApp.fetch(url);
var responseID3 = UrlFetchApp.fetch(url2);
var table1 = JSON.parse(responseID2.getContentText());
var table2 = JSON.parse(responseID3.getContentText());
var finalArray = [];
for(var i in table1){
for(var j in table2){
//Joining two tables
if(table1[i][0]==table2[j][0]){
// converting to json object
var newObj = {
"Item": table1[i][1],
"Date":table1[i][2],
"Order_NO": table2[j][1]
};
finalArray.push(newObj);
}
}
}
return finalArray;
Reduce the second table to a map :
const table2map = table2.reduce(
(mp,[codId,ordNo]) =>
mp.set(codId,
mp.has(codId)
? [...mp.get(codId),ordNo]
: [ordNo]
), new Map
)
Map table1 to a array of objects:
const finalArr = table1.map(
([id,Item,Date]) =>
({Item,Date,
"Order_No": table2map.get(id).join()})
)