SQL, extract unique values of JSON format field from each group #eg42

Below is a table in PostgreSQL. Its 1st column contains IDs, according to which rows are ordered. The 2nd column is the grouping column. The 3rd column contains JSON-like values (non-standard JSON), where there are duplicate items.

Image description
Use Java to do this: Extract unique items of the 3rd column values from each group and keep them at their original positions in the original records. The task can be interpreted like this: According to the order of records in each group, delete duplicate items downward and keep the unique items at their original positions.

Image description
Write the following SPL code:

Image description
A1: Query the database through JDBC.

A2: Split the 2nd field of each record into multiple items.

A3: Group records by the 1st column while keeping the original order, and handle each group: cumulate all items of the 2nd column for each record according to the order of records and remove duplicates; reverse the order of records; find difference between the cumulative result of the current record and that of the next record.

A4: Restore to the original string format and return the final result.

Read How to Call a SPL Script in Java to find how to integrate SPL into a Java application.

This is one of the problems on StackOverflow. You can click on it to see that the conventional solution is quite complicated, but the SPL approach is really simple and efficient.

SPL open source address


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Cloudystorage is also committed to being an environmentally friendly cloud storage provider.