首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataWeave groupBy和maxBy

DataWeave groupBy和maxBy
EN

Stack Overflow用户
提问于 2019-12-05 15:11:27
回答 2查看 447关注 0票数 0

我是个DataWeave新手。我试图为用户获取最新的扣减记录,但也按数组中的类型分组。这是我必须处理的数据:

代码语言:javascript
复制
"users": [
    {
        "employeeId": "123456",
        "lastName": "smith",
        "firstName": "joe ",
        "deductions": [
            {
                "deductionType": "ABC",
                "Amt": 1000,
                "StartDate": "2001-01-02T00:00:00"
            },
            {
                "deductionType": "ABC",
                "Amt": 1000,
                "StartDate": "2019-01-02T00:00:00"
            },
            {
                "deductionType": "ABC",
                "Amt": 1000,
                "StartDate": "2016-01-02T00:00:00"
            },
            {
                "deductionType": "DCA",
                "Amt": 4000,
                "StartDate": "2019-11-02T00:00:00",
            }
        ]
    }

我尝试使用类似的解决方案:Dataweave 2.0 maxBy and filter,但这似乎不起作用,因为我得到了一个空的有效负载。

最终结果应该如下所示:

代码语言:javascript
复制
    "users": [
    {
        "employeeId": "123456",
        "lastName": "smith",
        "firstName": "joe ",
        "deductions": [
            {
                "deductionType": "ABC",
                "Amt": 1000,
                "StartDate": "2019-01-02T00:00:00"
            },
            {
                "deductionType": "DCA",
                "Amt": 4000,
                "StartDate": "2019-11-02T00:00:00",
            }
        ]

我目前试图解决的办法是:

代码语言:javascript
复制
    payload.users map {($),
               deductions: (($.deductions groupBy $.deductionType) mapObject (value, key) -> 
                            {(key) : (value maxBy $.benefitStartDate)}) pluck (value) -> value

}

但这也没用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-05 16:09:03

尝尝这个

代码语言:javascript
复制
{ 
    users: payload.users map {
        ($ - "deductions"),
        deductions: (($.deductions groupBy $.deductionType) pluck $) map {
            ($ maxBy $.StartDate)
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-12-06 00:04:28

这里有一个不同的解决方案,使用性能更好的解决方案:)。该算法的注释可以在DW表达式中看到。此外,我在代码中硬编码了示例数据,因此您只需要复制和粘贴就可以使DW表达式工作。

代码语言:javascript
复制
%dw 2.0
output application/dw

var data = "users": [
    {
        "employeeId": "123456",
        "lastName": "smith",
        "firstName": "joe ",
        "deductions": [
            {
                "deductionType": "ABC",
                "Amt": 1000,
                "StartDate": "2001-01-02T00:00:00"
            },
            {
                "deductionType": "ABC",
                "Amt": 1000,
                "StartDate": "2019-01-02T00:00:00"
            },
            {
                "deductionType": "ABC",
                "Amt": 1000,
                "StartDate": "2016-01-02T00:00:00"
            },
            {
                "deductionType": "DCA",
                "Amt": 4000,
                "StartDate": "2019-11-02T00:00:00",
            }
        ]
    }
]
---
users: data.users map do {
    // Sort by StartDate, I type casted to a `DateTime` instead of comparing strings
    // Reverse the sorted list such that the latest dates are at the top of the list
    // Finally, get a set out the list where uniqueness is the deductionType
    //   since distinctBy maintains the first element that matches and removes the rest
    //   you know have a list of distinct deductionType with the latest date.       
    var ds = ($.deductions orderBy ($.StartDate as DateTime))[-1 to 0]
              distinctBy (d) -> d.deductionType
    ---
    {
        ($ - "deductions"),
        deductions: ds
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59198081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档