I have a table with 3 columns. I want make a SQL query that results in the maximum value in between the occurrences of DP_ID = A
| ID | DP_ID | Value |
|---|---|---|
| 1 | A | 10 |
| 2 | B | 264 |
| 3 | B | 265 |
| 4 | B | 266 |
| 5 | A | 10 |
| 6 | B | 115 |
| 7 | B | 116 |
| 8 | A | 25 |
The desired output would be:
| ID | DP_ID | Value |
|---|---|---|
| 4 | B | 266 |
| 7 | B | 116 |
I've tried to make a subquery to filter the table based on the ID of the rows that have DP_ID=A. The problem is that I can only retrieve one instance bye time. My desired output would list all the occurrences of it
Apache Derby is a quite limited database that does not support window functions or CTEs.
Note: I would strongly suggest you switch to H2 that has a similar footprint and offers much better capabilities.
The query becomes tediously long and non-performant in Derby, but can be written as:
Result:
The data script that demonstrates this example is: