执行GROUP BY后需要将类似客户添加到阵列中,但每个客户有限制 [英] Need to add similar customer into array after performing groupBy but with a limit per customer

查看:0
本文介绍了执行GROUP BY后需要将类似客户添加到阵列中,但每个客户有限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我问了一个类似的question,得到了正确的答案,但今天我面临了更多的问题。 因此,这是新的问题陈述。 我正在从Salesforce获取客户联系人,这些联系人以对象数组的形式出现,如下所示。

输入负载:

[
    {
        "customerID": 1,
        "customerName": "Jonhn1"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn2"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn3"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn4"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn5"
    },
    {
        "customerID": 2,
        "customerName": "Jonhn6"
    },
    {
        "customerID": 2,
        "customerName": "Jonhn7"
    },
    {
        "customerID": 2,
        "customerName": "Jonhn8"
    },
    {
        "customerID": 3,
        "customerName": "Jonhn9"
    },
    {
        "customerID": 3,
        "customerName": "Jonhn10"
    },
    {
        "customerID": 3,
        "customerName": "Jonhn11"
    },
    {
        "customerID": 4,
        "customerName": "Jonhn12"
    },
    {
        "customerID": 4,
        "customerName": "Jonhn13"
    },
    {
        "customerID": 5,
        "customerName": "Jonhn14"
    },
    {
        "customerID": 5,
        "customerName": "Jonhn15"
    },
    {
        "customerID": 5,
        "customerName": "Jonhn16"
    },
    {
        "customerID": 6,
        "customerName": "Jonhn17"
    },
    {
        "customerID": 7,
        "customerName": "Jonhn17"
    }
]

我需要输出是一个数组数组,每个子数组应该包含最多三个不同客户的所有客户详细信息。 我在上面的语句中得到了解决方案,如下所示

%dw 2.0
output application/json
import * from dw::core::Arrays
---
payload groupBy $.customerID pluck $ divideBy 3 map((flatten($)))

现在主要问题是 我们必须对阵列中的客户数量进行限制。例如,在ID为1的给定输入有效负载客户出现4(4)次以上时,该客户应该位于不同的数组中(即客户1的单独数组,例如5条记录的数组)。任何其他客户也可能发生这种情况。

因此,我们必须检查某个客户是否重复了4次以上,然后为该类型的客户创建单独的数组,否则按照前面的要求将其与其他客户组合

预期输出:

[
    [
        {
            "customerID": 1,
            "customerName": "Jonhn1"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn2"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn3"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn4"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn5"
        }
    ],
    [
        {
            "customerID": 2,
            "customerName": "Jonhn6"
        },
        {
            "customerID": 2,
            "customerName": "Jonhn7"
        },
        {
            "customerID": 2,
            "customerName": "Jonhn8"
        },
        {
            "customerID": 3,
            "customerName": "Jonhn9"
        },
        {
            "customerID": 3,
            "customerName": "Jonhn10"
        },
        {
            "customerID": 3,
            "customerName": "Jonhn11"
        },
        {
            "customerID": 4,
            "customerName": "Jonhn12"
        },
        {
            "customerID": 4,
            "customerName": "Jonhn13"
        }
    ],
    [
        {
            "customerID": 5,
            "customerName": "Jonhn14"
        },
        {
            "customerID": 5,
            "customerName": "Jonhn15"
        },
        {
            "customerID": 5,
            "customerName": "Jonhn16"
        },
        {
            "customerID": 6,
            "customerName": "Jonhn17"
        },
        {
            "customerID": 7,
            "customerName": "Jonhn18"
        }
    ]
]

推荐答案

请验证所有方案,如果我遗漏了什么,请纠正我。我只是根据单个数组的大小进行了聚合。

If Size[数组]>;4 ThendivideBy5 ElsedivideBy3.

distinctBy维护单个有效负载,就像我在IF和ELSE条件下使用的那样

注意-将不会像您提到的那样,在下雨的情况下维持秩序。

%dw 2.0
output application/json
import * from dw::core::Arrays
var a = payload groupBy $.customerID pluck $
---
flatten(a map
    (if (sizeOf($)>4)  
            $ divideBy sizeOf($) map(flatten($))
            else
    (a filter (sizeOf ($)< 5) divideBy 3 map (flatten ($))) 
    ) distinctBy $)

输出

[
  [
    {
      "customerID": 1,
      "customerName": "Jonhn1"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn2"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn3"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn4"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn5"
    }
  ],
  [
    {
      "customerID": 2,
      "customerName": "Jonhn6"
    },
    {
      "customerID": 2,
      "customerName": "Jonhn7"
    },
    {
      "customerID": 2,
      "customerName": "Jonhn8"
    },
    {
      "customerID": 2,
      "customerName": "Jonhn9"
    },
    {
      "customerID": 3,
      "customerName": "Jonhn10"
    },
    {
      "customerID": 3,
      "customerName": "Jonhn11"
    },
    {
      "customerID": 4,
      "customerName": "Jonhn12"
    },
    {
      "customerID": 4,
      "customerName": "Jonhn13"
    }
  ],
  [
    {
      "customerID": 5,
      "customerName": "Jonhn14"
    },
    {
      "customerID": 5,
      "customerName": "Jonhn15"
    },
    {
      "customerID": 5,
      "customerName": "Jonhn16"
    },
    {
      "customerID": 6,
      "customerName": "Jonhn17"
    },
    {
      "customerID": 7,
      "customerName": "Jonhn17"
    }
  ]
]

这篇关于执行GROUP BY后需要将类似客户添加到阵列中,但每个客户有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆