DEV Community

Judy
Judy

Posted on

4 1 1 1 1

SQL, Set different flags for different groups according to whether there are duplicate values #eg19

In MSSQL, my_table table has three columns (as shown below). Serial_Number is the grouping field; Id and Last_update_date contain detail data, and there are duplicate values in the last field.

Image description
We need to add a computed column named Flag. The rule is like this: Group rows by Serial_Number and record Flag value as "Y" for each record in this group if there are duplicate Last_update_date values; otherwise record Flag value as "N".

Image description
Write the following SPL code:

Image description
group()function groups rows without aggregation; @u option retains the original order of the records for the result. groups() function performs grouping and aggregation. ~ represents the current group/member; run()function modifies records in order. pselect() returns positions of members meeting the specified condition.

Source:https://stackoverflow.com/questions/78456365/column-comparison-between-rows-of-the-same-group-used-in-partition-by-clause

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
esproc_spl profile image
Judy

SPL open source address:github.com/SPLWare/esProc/stargazers

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay