[英]Joining two tables with many to many relationship fields
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.在这种情况下,第三方系统存在问题,因为它不允许 SQL 加入。 Therefore, I got the data by using 2 URLs separately and joined the two tables(customer_order table & Customer_Order_Demand table).因此,我分别使用 2 个 URL 获取数据并将两个表(customer_order 表和 Customer_Order_Demand 表)连接起来。 But the problem is there is a field(Order_NO) that has many to many relationships in one table.但问题是有一个字段(Order_NO)在一个表中有多对多的关系。 According to my code, I'm getting only first value in Order_No column after joining the two tables.根据我的代码,在加入两个表后,我只在 Order_No 列中获得第一个值。 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 :将第二个表减少为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: Map table1 到对象数组:
const finalArr = table1.map( ([id,Item,Date]) => ({Item,Date, "Order_No": table2map.get(id).join()}) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.