Selecting SQL from a group
Suppose we have the following table data:
ID parent stage submitted
1 1 1 1
2 1 2 1
3 1 3 0
4 1 4 0
5 5 1 1
6 5 2 1
7 5 3 1
8 5 4 1
As you can see, we have two groups (having the same parent). I want to select the last stage to be sent. In the example above, I want to select IDs 2 and 8. I am completely lost, so if anyone can help it would be greatly appreciated. :)
+1
a source to share
4 answers
SELECT T.ID, T.PARENT, T.STAGE
from
T,
(
select PARENT, MAX( STAGE) MAX_STAGE
from T
where SUBMITTED = 1
GROUP BY PARENT
) M
where
T.STAGE = M.MAX_STAGE
AND T.PARENT = M.PARENT
Explanation: First, isolate the maximum stage for each group with represented = 1 (internal selection). Then join the result with a real table to filter out records without a maximum step.
+8
a source to share