I have a table that collects securities holdings data for Funds like below.
How do I extract latest data for each FundName?
EffectiveDate | FundName | SecurityName | PercentageOfAssets |
---|---|---|---|
30/06/2022 | Fund A | xxx | 33.33% |
30/06/2022 | Fund A | yyy | 33.33% |
30/06/2022 | Fund A | zzz | 33.33% |
31/07/2022 | Fund B | xxx | 50% |
31/07/2022 | Fund B | yyy | 50% |
31/08/2022 | Fund B | yyy | 50% |
31/08/2022 | Fund B | zzz | 50% |
31/07/2022 | Fund A | xxx | 50% |
31/07/2022 | Fund A | yyy | 50% |
What I’m expecting
EffectiveDate | FundName | SecurityName | PercentageOfAssets |
---|---|---|---|
31/07/2022 | Fund A | xxx | 50% |
31/07/2022 | Fund A | yyy | 50% |
31/08/2022 | Fund B | yyy | 50% |
31/08/2022 | Fund B | zzz | 50% |
3
Answers
This will get the expected output
You can do it with
cross apply
in SQL Server:Create a subquery that will get the
MAX
EffectiveDate
thenJOIN
it to an outer query to return the rest of your rows:Result:
Fiddle here.
Note: This will work for both MySQL and SQL Server.