Mule 根据类别压缩数据

发布于 2025-01-10 11:44:28 字数 1101 浏览 0 评论 0原文

下面的例子。我有一组帐号和帐户属性。对于每个 account_number 都有三个类别,我希望根据 DataWeave 中的每个余额计算每个帐号的总和。

数据输入

[
    {
        Account_Number: 1,
        Account: 5,
        Category: "A",
        Balance: 500
    },
    {
        Account_Number: 1,
        Account: 5,
        Category: "A",
        Balance: 700
    },
    {
        Account_Number: 1,
        Account: 5,
        Category: "B",
        Balance: 300
    },
    {
        Account_Number: 1,
        Account: 5,
        Category: "C",
        Balance: 100
    },
    {
        Account_Number: 2,
        Account: 10,
        Category: "B",
        Balance: 300
    },
    {
        Account_Number: 2,
        Account: 10,
        Category: "B",
        Balance: 800
    }
]

数据输出

[
    {
        Account_Number: 1,
        Account: 5,
        CategoryA_Balance: 1200,
        CategoryB_Balance: 300,
        CategoryC_Balance: 100
    }
    {
        Account_Number: 2,
        Account: 10,
        CategoryA_Balance: 0,
        CategoryB_Balance: 1100,
        CategoryC_Balance: 0
    }
]```

Example below. I've got a set of account numbers, with an account attribute. For each account_number there are three categories, and I would like the sum for each account number based on each balance in DataWeave.

Data input

[
    {
        Account_Number: 1,
        Account: 5,
        Category: "A",
        Balance: 500
    },
    {
        Account_Number: 1,
        Account: 5,
        Category: "A",
        Balance: 700
    },
    {
        Account_Number: 1,
        Account: 5,
        Category: "B",
        Balance: 300
    },
    {
        Account_Number: 1,
        Account: 5,
        Category: "C",
        Balance: 100
    },
    {
        Account_Number: 2,
        Account: 10,
        Category: "B",
        Balance: 300
    },
    {
        Account_Number: 2,
        Account: 10,
        Category: "B",
        Balance: 800
    }
]

Data Output

[
    {
        Account_Number: 1,
        Account: 5,
        CategoryA_Balance: 1200,
        CategoryB_Balance: 300,
        CategoryC_Balance: 100
    }
    {
        Account_Number: 2,
        Account: 10,
        CategoryA_Balance: 0,
        CategoryB_Balance: 1100,
        CategoryC_Balance: 0
    }
]```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2025-01-17 11:44:28

我认为类别是动态的。如果没有,您可以用静态数组替换类别变量。

%dw 2.0
output application/json
var byAcctNbr = payload groupBy ($.Account_Number)
var categories = payload..Category distinctBy $
---
keysOf(byAcctNbr) map ((acctNbr) -> 
    do {
        var item = byAcctNbr[acctNbr] 
        var outItem = (item[0] default {}) - "Balance" - "Category"

        var balances = categories reduce ((category, acc={}) ->
            do {
                var accounts = item filter ($.Category == category)
                ---            
                acc ++ (
                        ("Category" ++ category ++ "_Balance"): if (isEmpty(accounts)) 0 
                                                                else sum (accounts.Balance)
                    )

        })
        ---
        outItem ++ balances
    }
)

I assume Categories are dynamic. If not, you can replace the Categories variable with a static array.

%dw 2.0
output application/json
var byAcctNbr = payload groupBy ($.Account_Number)
var categories = payload..Category distinctBy $
---
keysOf(byAcctNbr) map ((acctNbr) -> 
    do {
        var item = byAcctNbr[acctNbr] 
        var outItem = (item[0] default {}) - "Balance" - "Category"

        var balances = categories reduce ((category, acc={}) ->
            do {
                var accounts = item filter ($.Category == category)
                ---            
                acc ++ (
                        ("Category" ++ category ++ "_Balance"): if (isEmpty(accounts)) 0 
                                                                else sum (accounts.Balance)
                    )

        })
        ---
        outItem ++ balances
    }
)
伪装你 2025-01-17 11:44:28

与 sudhish 类似的解决方案。分解解决方案以更好地理解

distinctBy 因为 .. 将为您提供输入中存在的所有类别。 DistinctBy 将删除重复项,您将有 [A,B,C]

groupBy 根据每个帐号的详细信息进行分组

(item[0] - "Balance" - "Category")< /code> 由于我们只需要 AccountNumber 和 Account 一次,因此使用 item[0] 和“-”来消除 Balance 和 Category,因为我们需要进一步执行一些基于条件的逻辑

pluck 来转换带有 account 的对象数字作为数组的键

map 迭代每个帐号的详细信息

map 遍历类别将产生两个帐号的 [A,B,C]

filter检查顶级映射中存在的类别是否与变量中存在的类别匹配。 if (true) then sum(Balance) else 0

sum 根据使用过滤器匹配的类别进行添加

%dw 2.0
output application/json
var categories = payload..Category distinctBy $
--- 
payload groupBy $.Account_Number pluck $ map(item,index)->{
    (item[0] - "Balance" - "Category"),
    (categories map (cat)->{
        ("Category" ++ cat ++ "_Balance"):
            if (isEmpty(item filter ($.Category == cat)))
                 0
            else
               sum((item filter ($.Category == cat)).Balance)
    })
}  

A Similar solution to sudhish. Breaking down the solution for better understanding

distinctBy Since .. will give you all the categories present in the input. DistinctBy will remove duplicates and you will have [A,B,C]

groupBy to group based over details of each account number

(item[0] - "Balance" - "Category") Since we require AccountNumber and Account only once so used item[0] and "-" to eliminate Balance and Category since we need to perform some conditional based logic further

pluck to convert the object with account number as key to array

map iterates over the details of each account number

map over the categories will yield you [A,B,C] for both the account numbers

filter to check if the Category present in the top level map matches the categories present in the variable. if (true) then sum(Balance) else 0

sum to add based on the categories matched using filter

%dw 2.0
output application/json
var categories = payload..Category distinctBy $
--- 
payload groupBy $.Account_Number pluck $ map(item,index)->{
    (item[0] - "Balance" - "Category"),
    (categories map (cat)->{
        ("Category" ++ cat ++ "_Balance"):
            if (isEmpty(item filter ($.Category == cat)))
                 0
            else
               sum((item filter ($.Category == cat)).Balance)
    })
}  

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文