I have the following Table A & Table B, where A is one to many with B; for each record of A, i need to select the corresponding max record on B for each group for A.
i.e. grouping is done based on last_updated_time.
SELECT taba.ws_name, tabb.b2a, max(tabb.last_update_time)
FROM TabA taba, TabB tabb
where taba.name = 'xyz'
and taba.id = tabb.b2a
group by taba.ws_name, tabb.b2a
This works well and fine.
The problem arises when i need to select the “status” as well of the last transaction, this leads to selection of duplicates because status can either be failed or success.
My requirement is to select only the max record irrespective of its status (though i need to display the status as well)
So when ever any “ws_name” has multiple records of failure as well as success so the MAX updated_time gets selected for both groups (of “Failure” & “Success”).
Below is the query which i tried and havent got clue on how to remove the duplicates selected because of status.
SELECT taba.ws_name, tabb.b2a, **tabb.status**, max(tabb.last_update_time)
FROM TabA taba, TabB tabb
where taba.name = 'xyz'
and taba.id = tabb.b2a
group by taba.ws_name, tabb.b2a, **tabb.status**
Usually, you need to select the max values, then join onto this result to get the actual values you want.
For example:
InnerQuery gives you all the rows you want, except without the status. We therefore join back onto B to get the status for the matching rows.
i.e. we get all the rows in B we want then join back onto B to get the status value for those rows that we’ve uniquely identified, because, as you say, introducing status just gives us another level of grouping.