I have the following data in one of my tables:
Id subpartID MainpartID EngagementName HashValue Version CreatedDate ModifiedDate
--------------------------------------------------------------------------------------------
1 IN-2022 111112 name1 hash1 1 2022-05-26 NULL
2 IN-2022 111112 name2 hash2 2 2022-05-26 NULL
3 JP-2022 221112 name3 hash3 1 2022-05-26 NULL
4 JP-2022 221112 name4 hash4 2 2022-05-26 NULL
5 AU-2022 221112 name5 hash5 1 2022-05-26 NULL
I want to fetch records from it. When I enter a MainpartID = 111112
, I want the output to be something like this:
Id subpartID MainpartID EngagementName HashValue Version CreatedDate ModifiedDate
--------------------------------------------------------------------------------------------
2 IN-2022 111112 name2 hash2 2 2022-05-26 NULL
and when I enter MainpartID = 221112
, I want the output to look like this:
Id subpartID MainpartID EngagementName HashValue Version CreatedDate ModifiedDate
--------------------------------------------------------------------------------------------
4 JP-2022 221112 name4 hash4 2 2022-05-26 NULL
5 AU-2022 221112 name5 hash5 1 2022-05-26 NULL
What the logic here is that for a particular MainpartID, fetch all subpartID's and then in the final output show only the latest version of a particular subpartID.
My code is:
SELECT *
FROM egTable
WHERE egTable.[Version] = (SELECT MAX([Version])
FROM egTable
WHERE MainpartID = '111112'
GROUP BY subpartID)
but this doesn't seem to work.
Can someone help me with this? Data is dummy.