cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Help Using Group By!

andre_mangatal
New Contributor

Dear Community

I need your help with understanding the group by in the attached simple pipeline.

I am supplying the Json data and sorting and grouping it by ID as in the picture but for each Group, i.e. ID. Then I need to first determine if a type of address exists e.g. โ€œBUSโ€ business or โ€œRESโ€ residential and then transform, e.g. via a mapper, to output both the business and residential addresses to Mapper variables $Business and $Residential output as one document/record

Thank you very much for your help in advance,
Andre M

JsonSAMPLE groupBy_Sample

13 REPLIES 13

Hi tstack,

In the group if I have a date field and I wanted to get the record with the latest date returned, how would I do that?

match $group {
[โ€ฆ, { EndDate: โ€œ2019-8-31โ€, Value }, โ€ฆ] => Value
}

Many thanks

Can you clarify a couple of things:

  • Are the date fields strings or Date objects?
  • Are the elements of $group already sorted by date?

andre_mangatal
New Contributor

Hi tstack

Thank you for the reply. I need to get a solution for this. The date fields are strings and they are sorted descending before i did the group by via JDBC execute. A sample of the json just after the group by is shown below. I am grouping by โ€œPOL_IDโ€ then โ€œCVG_NUMโ€ but I need to return ONLY the first record in each group i.e. with the latest date. How can I do this please??

[
{โ€œgroupByโ€:{โ€œCVG_NUMโ€:โ€œ01โ€,โ€œPOL_IDโ€:โ€œDT90000051โ€},โ€œgroupโ€:[{โ€œCVG_NUMโ€:โ€œ01โ€,โ€œPOL_IDโ€:โ€œDT90000051โ€,โ€œCO_IDโ€:โ€œ01โ€,โ€œFIA_EFF_DTโ€:โ€œ2010-04-30โ€}]},{โ€œgroupByโ€:{โ€œCVG_NUMโ€:โ€œ02โ€,โ€œPOL_IDโ€:โ€œDT90000051โ€},โ€œgroupโ€:[{โ€œCVG_NUMโ€:โ€œ02โ€,โ€œPOL_IDโ€:โ€œDT90000051โ€,โ€œCO_IDโ€:โ€œ01โ€,โ€œFIA_EFF_DTโ€:โ€œ2010-04-30โ€}]},{โ€œgroupByโ€:{โ€œCVG_NUMโ€:โ€œ01โ€,โ€œPOL_IDโ€:โ€œDT90000051โ€}]}]

If you just need the first element of the group, you can index it directly, like so:

$group[0]

If youโ€™re wanting to deconstruct it with a match expression, you can match the first element by making a pattern like the following:

match $group {
    [{ CO_ID, FIA_EFF_DT }, โ€ฆ] => CO_ID + " " + FIA_EFF_DT
}

When writing a pattern for match, write something that looks like the thing youโ€™re trying to match and use ... as a wildcard. So, the above pattern will match an array with at least one object element that has the given properties.