首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据编织-如何才能得到订单Mule 4的总数?

数据编织-如何才能得到订单Mule 4的总数?
EN

Stack Overflow用户
提问于 2022-10-20 21:03:29
回答 3查看 41关注 0票数 0

我有以下情况和问题,我使用CSV文件并与DW进行映射,按"PON“列分组,我需要得到此列的总顺序乘此列( Qty * Price ),我没有正确的结果,我将向您展示:

CSV数据:

代码语言:javascript
复制
PON,Item,Qty,Price
PON1000,2015,2,38.08
PON1000,2016,1,33.37
PON1001,2015,2,38.08

DW:

代码语言:javascript
复制
%dw 2.0
output application/json
---
payload groupBy ($.PON) pluck $ map ( () -> {
   "order": $[0].PON default "",
   "total": (sum( $.Price filter ($ != "") ) as Number) as String {format: "##,###.00"},
   "products": $ map {
       "product": $.Item,
       "price": ($.Price as Number) as String {format: "##,###.00"},
       "quantity": $.Qty
   }
})

取得的结果:

代码语言:javascript
复制
[
 {
   "order": "PON1000",
   "total": "71.45",
   "products": [
    {
     "product": "2015",
     "price": "38.08",
     "quantity": "2"
    },
    {
     "product": "2016",
     "price": "33.37",
     "quantity": "1"
    }
   ]
   },
   {
    "order": "PON1001",
    "total": "38.08",
    "products": [
    {
      "product": "2015",
       "price": "38.08",
       "quantity": "2"
     }
    ]
    }
   ]

我需要按顺序乘以“价格”*“数量”对应的值,最后将该值和该值之和,并按顺序将该值列在“总量”一栏中。

预期结果:

代码语言:javascript
复制
[
 {
   "order": "PON1000",
   "total": "109.53",
   "products": [
    {
     "product": "2015",
     "price": "38.08",
     "quantity": "2"
    },
    {
     "product": "2016",
     "price": "33.37",
     "quantity": "1"
    }
   ]
   },
   {
    "order": "PON1001",
    "total": "76.16",
    "products": [
    {
      "product": "2015",
       "price": "38.08",
       "quantity": "2"
     }
    ]
    }
   ]

任何帮助都将不胜感激。谢谢。

最诚挚的问候!

EN

回答 3

Stack Overflow用户

发布于 2022-10-20 21:22:10

我只是补充了两件事:

  1. 表示我使用了Arrays::sumBy,它让我们对每一项进行操作,并对所有结果进行求和。
  2. I从代码中推断出价格是空的,所以我创建了safeNumber()来检查它,如果它是空的(默认情况下不工作),返回0。

这是我到目前为止掌握的代码:

代码语言:javascript
复制
%dw 2.0
output application/json
import * from dw::core::Arrays
fun safeNumber(str) = if(isEmpty(str)) 0 else str as Number
---
payload groupBy ($.PON) pluck $ map ( () -> {
   "order": $[0].PON default "",
   "total": ($ sumBy( (i) -> safeNumber(i.Qty) * safeNumber(i.Price))) as String {format: "##,##0.00"},
   "products": $ map {
       "product": $.Item,
       "price": safeNumber($.Price) as String {format: "##,##0.00"},
       "quantity": $.Qty
   }
})
票数 1
EN

Stack Overflow用户

发布于 2022-10-25 12:12:31

下面的脚本将帮助您。

代码语言:javascript
复制
%dw 2.0
output application/json
import divideBy from dw::core::Objects

---
payload groupBy $.PON  mapObject ((value, key, index) -> 
({
   order: (key),
   total: sum(value map ($.Qty  * $.Price)),
   value : value
})
) divideBy 3
票数 0
EN

Stack Overflow用户

发布于 2022-10-31 09:48:17

您可以使用以下DataWeave表达式:

代码语言:javascript
复制
%dw 2.0
output application/json
---
payload groupBy $.PON pluck $ map (() -> {
    "order": $[0].PON,
   "total": sum($ map ($.Qty as Number * $.Price as Number)),
   "products": $ map { 
       "product": $.Item,
       "price": $.Price as Number,
       "quantity": $.Qty as Number
    }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74146293

复制
相关文章

相似问题

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