How can I call op.arrayAggregate method to return multiple columns

79 Views Asked by At

I tried to group below response by name:

{
  "document1": [
    {
      "row": {
        "schema": "Schemas",
        "view": "CustomerAddress",
        "data": {
          "rownum": "1",
          "CustomerName": "Name1",
          "AddrTypeCd": "MailingAddress",
          "Addr1": "911 FORBES AVE",
          "Addr2": "SUITE XXX",
          "CityName": "asdfasfd",
          "StateProvCd": "PA",
          "PostalCode": "15219"
        }
      }
    },
    {
      "row": {
        "schema": "Schemas",
        "view": "CustomerAddress",
        "data": {
          "rownum": "2",
          "CustomerName": "Name1",
          "AddrTypeCd": "OfficeAddress",
          "Addr1": "911 Watson AVE",
          "Addr2": "SUITE XXX",
          "CityName": "asdfasfd",
          "StateProvCd": "CT",
          "PostalCode": "15119"
        }
      }
    }
  ],
  "document2": []
}

I used arrayAggregate method in this way

.groupBy(customerAddreses.col('CustomerName'), 
         op.arrayAggregate("Customer addresses", customerAddreses.col('Addr1')))

and my current response data look like this:

{
"schemas.customerAddreses.CustomerName": "Name1", 
"Customer addresses": [
   "911 FORBES AVE", 
   "911 Watson AVE"
 ]
}

Is it possible to call arrayAggregate with mutiltiple columns and receive object in Customer addresses array? Below is the response what I want to obtain:

{
    "schemas.customerAddreses.CustomerName": "Name1",
    "Customer addresses": [
        {
            "Addr1": "911 FORBES AVE",
            "Addr2": "SUITE XXX",
            "CityName": "asdfasfd",
            "StateProvCd": "PA",
        },
        {
            "Addr1": "911 Watson AVE",
            "Addr2": "SUITE XXX",
            "CityName": "asdfasfd",
            "StateProvCd": "CT"
        }
    ]
}
1

There are 1 best solutions below

0
Fiona Chen On BEST ANSWER

Input

{
    "document1": [
        {
            "data": {
                "rownum": "1",
                "CustomerName": "Name1",
                "AddrTypeCd": "MailingAddress",
                "Addr1": "911 FORBES AVE",
                "Addr2": "SUITE XXX",
                "CityName": "asdfasfd",
                "StateProvCd": "PA",
                "PostalCode": "15219"
            }
        },
        {
            "data": {
                "rownum": "2",
                "CustomerName": "Name1",
                "AddrTypeCd": "OfficeAddress",
                "Addr1": "911 Watson AVE",
                "Addr2": "SUITE XXX",
                "CityName": "asdfasfd",
                "StateProvCd": "CT",
                "PostalCode": "15119"

            }
        }
    ]
}

JavaScript Optic

op.fromView('main', 'CustomerAddress')
    .select(['Addr1',
             'Addr2',
             'CityName',
             'StateProvCd',
             'CustomerName',   
              op.as('addrAggr', op.jsonObject([
                op.prop('Addr1', op.col('Addr1')),
                op.prop('Addr2',  op.col('Addr2')),
                op.prop('CityName', op.col('CityName')),
                op.prop('StateProvCd', op.col('StateProvCd'))
              ]))
          ])
    .orderBy(op.asc('Addr1'))   //Line14
    .groupBy('CustomerName', op.arrayAggregate("Customer addresses", "addrAggr"))
    .result();

Result

{
    "CustomerName": "Name1", 
    "Customer addresses": [
        {
            "Addr1": "911 FORBES AVE", 
            "Addr2": "SUITE XXX", 
            "CityName": "asdfasfd", 
            "StateProvCd": "PA"
        }, 
        {
            "Addr1": "911 Watson AVE", 
            "Addr2": "SUITE XXX", 
            "CityName": "asdfasfd", 
            "StateProvCd": "CT"
        }
    ]
}

Note

  1. Your TDE view name one is CustomerAddress while another customerAddreses. Not sure how you could get any result.
  2. To mirror SQL-like ORDER, please add sort clause (Line14) in the Optic.
  3. JSON root property name should be consistent. Perhaps, address instead of document1.