SELECT SUM(CD.[Value]) AS [Value]    FROM [{schema}].[CompanyData] CD        INNER JOIN #Locations T            ON T.LocationId = CD.CompanyId        INNER JOIN (            SELECT CDM.CompanyId, MAX(CDM.[Date]) as MaxDate            FROM [{schema}].[CompanyData] CDM                INNER JOIN #Locations T                    ON T.LocationId = CDM.CompanyId            GROUP BY CDM.CompanyId        ) TM             ON CD.CompanyId = TM.CompanyId AND CD.[Date] = TM.MaxDateI have query to select SUM from latest values for each row with different CompanyId.Is this double join of same tables (Query and subQuery) fine or I can improve somehow it? Thanks.
Select sum from latest values in table
sql;sql server;join