cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the sequence for a group of values

sdeng
New Contributor II

Like how to get AddressNO group by the same IDs like below?
image

1 ACCEPTED SOLUTION

tlikarish
Employee
Employee

Oh, sorry for the misunderstanding. You want to generate the AddressNO based on the number of elements for each ID?

You’ll have to modify the sort and group by to do so by ID. Then afterwards you’ll need a Mapper snap using the map expression.

$group.map((e, index) => e.extend({'AddressNO': index + 1}))

For each element that was group, this will extend it with the index in the group array.

image

Finally now that the elements have an AddressNO you can split everything back out using the JSONSplitter snap.

I’ve updated the pipeline so you can try it out yourself.

groupby-example_2018_10_26.slp (8.0 KB)

View solution in original post

4 REPLIES 4

tlikarish
Employee
Employee

If I understand your question, you’re trying to group your data set by the AddressNO field. That is, the expected output would have all rows with AddressNO equal to one grouped, and all AddressNO equal to two grouped, etc.

If that’s the case, then you’ll need to sort on AddressNO and then use a group by field snap.

image

Here is the pipeline in my example if you want to upload it and try it out.

groupby-example_2018_10_25.slp (5.1 KB)

sdeng
New Contributor II

Yes, I can do a group by, but what I want is to provide the sequence for the rows in one group. The source file only have IDs and address, how to get addressno?

tlikarish
Employee
Employee

Oh, sorry for the misunderstanding. You want to generate the AddressNO based on the number of elements for each ID?

You’ll have to modify the sort and group by to do so by ID. Then afterwards you’ll need a Mapper snap using the map expression.

$group.map((e, index) => e.extend({'AddressNO': index + 1}))

For each element that was group, this will extend it with the index in the group array.

image

Finally now that the elements have an AddressNO you can split everything back out using the JSONSplitter snap.

I’ve updated the pipeline so you can try it out yourself.

groupby-example_2018_10_26.slp (8.0 KB)

sdeng
New Contributor II

Thanks, it’s work