English 中文(简体)
Can we add multiple conditions on left join in Mulesoft
原标题:

Array 1:

[
        { 
            "name": "Abc", 
            "address":"Mumbai",
            "phone":"91-1234567891",
            "id":123
        }, 
        { 
            "name": "Pqr", 
            "address":"Pune",
            "phone":"91-1234985438",
            "id":456
        }, 
        { 
            "name": "Abc",
            "address":"Delhi",
            "phone":"91-1234567891",
            "id":123
        }
    ]

Array 2:

[
    { 
        "sid":123,
        "postalCode": "67890",
        "country": "USA",
        "sname": "tyu"
    }, 
    { 
        "sid":864,
        "postalCode": "54983",
        "country": "UK",
        "sname": "Pqr"
    }, 
    { 
        "sid":123,
        "postalCode": "27932",
        "country": "EU",
        "sname": "Uvg"
    }
]

Above we have to consider both ID and name, if either of the fields match as leftjoin then we have to consider them.

Expected Output:

[
  {
    "name": "Abc",
    "address": "Mumbai",
    "phone": "91-1234567891",
    "id": 123,
    "sid": 123,
    "sname": "tyu",
    "postalCode": "27932",
    "country": "EU"
  },
  {
    "name": "Pqr",
    "address": "Pune",
    "phone": "91-1234985438",
    "id": 456,
    "sid": 864,
    "sname": "Pqr",
    "postalCode": "54983",
    "country": "UK"
  },
  {
    "name": "Abc",
    "address": "Delhi",
    "phone": "91-1234567891",
    "id": 123,
    "sid": 123,
    "sname": "Uvg",
    "postalCode": "27932",
    "country": "EU"
  }
]

Dataweave:

%dw 2.0
output application/json
import * from dw::core::Arrays
var Array2reversed=Array2[-1 to 0] distinctBy ((item, index) -> item.sid)
var combine=leftJoin(Array1, Array2reversed, (Array1) -> Array1.name, (Array2) -> Array2.sname) map $
---
(leftJoin(combine, Array2reversed, (Array1) -> Array1.id, (Array2) -> Array2.sid)) 
    map {
        "name": $.l.name, 
        "address": $.l.address,
        "phone": $.l.phone,
        "id": $.l.id,
        "sid": $.r.sid,
        "postalCode": $.r.postalCode,
        "country": $.r.country
    }.

We have to consider name and sname as another condition in left join. I tried to give in function but I m getting error and not able to join with multiple conditions in one join only.

问题回答

You can try this.

%dw 2.0
var Array1 = [
        { 
            "name": "Abc", 
            "address":"Mumbai",
            "phone":"91-1234567891",
            "id":123
        }, 
        { 
            "name": "Pqr", 
            "address":"Pune",
            "phone":"91-1234985438",
            "id":456
        }, 
        { 
            "name": "Abc",
            "address":"Delhi",
            "phone":"91-1234567891",
            "id":123
        }
    ]
var Array2 = [
    { 
        "sid":123,
        "postalCode": "67890",
        "country": "USA",
        "sname": "tyu"
    }, 
    { 
        "sid":864,
        "postalCode": "54983",
        "country": "UK",
        "sname": "Pqr"
    }, 
    { 
        "sid":123,
        "postalCode": "27932",
        "country": "EU",
        "sname": "Uvg"
    }
]
output application/json
import * from dw::core::Arrays
var Array2reversed=Array2[-1 to 0] distinctBy ((item, index) -> item.sid)
var combine = leftJoin(Array1, Array2reversed, (Array1) -> Array1.name, (Array2) -> Array2.sname) map ((item, index) -> item.l ++ (item.r default {}))

---
(leftJoin(combine, Array2reversed, (Array1) -> Array1.id, (Array2) -> Array2.sid)) 
    map {
        "name": $.l.name, 
        "address": $.l.address,
        "phone": $.l.phone,
        "id": $.l.id,
        "sid": $.r.sid  default $.l.sid,
        "sname": $.r.sname  default $.l.sname,
        "postalCode": $.r.postalCode default $.l.postalCode,
        "country": $.r.country default $.l.country
    }

Output

[
  {
    "name": "Abc",
    "address": "Mumbai",
    "phone": "91-1234567891",
    "id": 123,
    "sid": 123,
    "sname": "Uvg",
    "postalCode": "27932",
    "country": "EU"
  },
  {
    "name": "Pqr",
    "address": "Pune",
    "phone": "91-1234985438",
    "id": 456,
    "sid": 864,
    "sname": "Pqr",
    "postalCode": "54983",
    "country": "UK"
  },
  {
    "name": "Abc",
    "address": "Delhi",
    "phone": "91-1234567891",
    "id": 123,
    "sid": 123,
    "sname": "Uvg",
    "postalCode": "27932",
    "country": "EU"
  }
]




相关问题
trrying to optimize the query

I am just trying to left join and attach all the table but remaining table seems to be same is there any other way to attach the table like function or temp table to optimize it. SELECT Business ...

Slow query - Multiple joins and a lot of data

I m sure there is a better way I could be doing this. I have three main tables with large amounts of data to run through: records_main, sales, and appointments. Each with close to 20,000 records. I ...

Is an outer join possible with Linq to Entity Framework

There are many examples of outer join using Linq to Sql, all of them hinging on DefaultIfEmpty() which is not supported with Linq to Entity Framework. Does this mean that outer join is not possible ...

MYSQL DATE function running insanely slow in LEFT JOIN

When adding the line: LEFT JOIN core_records_sales as sales ON DATE(appointments.date) = DATE(sales.date_sold) To my query, it boosts the time for the script to run from about 8 seconds, to 2-3 ...

Limiting Access by Permission

thanks for viewing this. I have a db that has users, roles & user_roles. What I am trying to achieve is a login that will select users who have Admin or Associate permissions. The login then uses ...

热门标签