Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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. 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;
  
  
 

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

  • 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()})
    )
    
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda