Determine whether to filter again based on the situation in the filtered result set

A table in the DB2 database is as follows:

ID

ABBREV

1

A

1

B

2

B

2

C

3

A

3

A

3

C

Now we need to preliminarily filter out records with IDs equal to the specified parameters, and further process them based on whether the ABBREV field in the result set contains "A". If it contains "A", we will further filter out records that only contain "A" and return them; When 'A' is not included, return the preliminary filtering result directly.

For example, when the parameter is equal to 1, it should return:

ID

ABBREV

1

A

When the parameter is equal to 2, return:

ID

ABBREV

2

B

2

C

When the parameter is equal to 3, return:

ID

ABBREV

3

A

3

A

SPL code:


 A

1

=db2.query("select * from tb where ID=?",argID)

2

=if((t=A1.select(ABBREV=="A"))==[],A1,t)

A1: Use JDBC to query the database and preliminarily filter out records with IDs equal to the specified parameter.

A2: Further filter records with ABBREV equal to "A". If the result is an empty set, return A1; If the result is not an empty set, return the result of further filtering.

Question sourcehttps://stackoverflow.com/questions/78371930/db2-filter-sql-for-condition