DEV Community

Judy
Judy

Posted on

2 1 1 1 1

How to check the same status of data in each row within a group with esProc

The ID field of a certain database table is the grouping field of cars, and each group of cars is further subdivided into brand and type.

Image description
Now we need to group the cars by ID and calculate whether the difference between the cars in the group is in terms of brand or type. If there is more than one brand of car in the group, we will assign the difference column to Brand; If there is more than one type of car in the group, assign the difference as Type.

Image description
After SQL grouping, it is necessary to aggregate immediately, making it difficult to make logical judgments in the grouped subset, and the indirectly implemented code is very complex. SPL can retain the grouped subset for further calculation:
Try.DEMO

Image description
A1: Load data.

A2: Use the group function to group by ID, but do not aggregate. Each group is a set of records.

Image description
A3: Process each group of data: If the Brand of the current group is greater than 1 after count distinct, create a new record with the field ID taken from the current group and the field Difference as the string “Brand”; Similar processing is applied to the Type field of the current group, but the Difference field of the new record is the string ‘Type’. The symbol ~ represents the current group, the icount function is used for count distinct, and the symbol | can merge new records (or any data) into a set.

Image description
A4: Merge members of different groups.

Image description
The above A2-A4 are calculated step by step for easy debugging, and can also be combined into one statement:

=A1.group(ID). conj(if(~.icount(Brand)>1,new(ID,"Brand":Difference))|if(~.icount(Type)>1,new(ID,"Type":Difference)))
Enter fullscreen mode Exit fullscreen mode

esProc is open source and free. Download~~

Postmark Image

The email service that speaks your language

Whether you code in Ruby, PHP, Python, C#, or Rails, Postmark's robust API libraries make integration a breeze. Plus, bootstrapping your startup? Get 20% off your first three months!

Start free

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free