Using SQL Server 2012 I have a table called Allbucket
CustodianAccountNum symbol EndDate ManagerName MarketValue NetReturn
A9G040819 wabix 12/31/2013 GMO Benchmark 34751.10987 0.004072
A9G040819 wabix 1/31/2014 GMO Benchmark 34128.88767 -0.017905
A9G040819 wabix 2/28/2014 GMO Benchmark 49969.8081 0.0202
A9G040819 wabix 3/31/2014 GMO Benchmark 50370.993 0.008028
A9G040819 wabix 4/30/2014 GMO Benchmark 50995.0584 0.012389
A9G040819 amj 12/31/2013 JPMorgan Alerian 1234.55 -0.008154
A9G040819 amj 2/28/2014 JPMorgan Alerian 14849.76 -0.018599
A9G040819 amj 3/31/2014 JPMorgan Alerian 14892.8 0.015203
A9G040819 amj 4/30/2014 JPMorgan Alerian 15513.6 0.041684
I am trying to load this data from one system to another. However it requires per a given CustodianAccountNum, for all symbols to have the same date intervals during periods of time where they both exist.
Note that amj is missing 1/31/2014. The clue is that at least one other security, in this case wabix has that date during the same span of time. Also note sometimes dates are intramonth such as 1/15/2014
I was hoping to do something like a self join and partition where I take all possible distinct dates for a given CustodianAccountNum then force all rows to have the same periodicity during the span of time they overlap. For the interpolated rows that were not original and 'borrowed' from another symbol existing in that span of time I would like to pull the LAG marketvalue from the previous row of that symbol (if a previous row exists if not then 0) and force all other values to zero. There are additional columns in the original data but I'm trying to keep this example simple.
So Ideally AMJ would look like this because wabix has the 1/31/2014 date
CustodianAccountNum symbol EndDate ManagerName MarketValue NetReturn
A9G040819 amj 12/31/2013 JPMorgan Alerian 1234.55 -0.008154
A9G040819 amj 1/31/2014 JPMorgan Alerian 1234.55 -0.0
A9G040819 amj 2/28/2014 JPMorgan Alerian 14849.76 -0.018599
A9G040819 amj 3/31/2014 JPMorgan Alerian 14892.8 0.015203
A9G040819 amj 4/30/2014 JPMorgan Alerian 15513.6 0.041684
the guiding principle of missing dates is if any other Symbol has a date paritioned by a given custodianaccountnum. There are thousands of different accountnums but they only need to align per a given accountnum
My concern is only with date gaps during the lifetime of the Symbol per account. If another symbol name exists years before it I don't need many months of 0 added. I just need them in sync from the first to the last date of a given symbol across all Symbols that overlap in time.
UPDATE
Gordon Linoff's reply gets me close but not quite there. I had to change OUTER APPLY to CROSS apply or I was getting thousands of null records in all columns.
I've amended the query to show all the desired columns however this query is resulting in all but market value being = 0. Basically I want to force all the values to 0 for the derived row (1/31/2014 in my example) except market vlaue which I'd like to pull from the previous market value. However for all non derived rows I want to use the original values across the entire row.
select
ab.drank,d.EndDate,ab.BranchName,ab.EntityID,ab.CustodianAccountNum,ab.AccountID,ab.ManagerName,
ab.FTAssetStyle,ab.FTAssetClass,ab.PWMSecurityID,ab.AssetClassCode,ab.AssetClass,ab.Symbol,ab.SecType,
ab.Cusip,ab.Held,ab.MarketValue,
0 AS GrossFlow,0 AS GrossWeight,0 AS GrossReturn,0 AS NetFlow,0 AS NetWeight,
0 AS NetReturn,0 AS PortfolioFees,0 AS PortfolioExpenses,0 AS ManagerFees,0 AS Income
from (select distinct CustodianAccountNum, enddate from Allbucket) d join
(select distinct CustodianAccountNum, symbol from Allbucket) s
on d.CustodianAccountNum = s.CustodianAccountNum CROSS apply
(select top 1 ab.*
from Allbucket ab
where d.CustodianAccountNum = ab.CustodianAccountNum and
d.enddate <= ab.enddate and
s.symbol = ab.symbol
AND ab.CustodianAccountNum = 'A9G040819'
order by d.enddate desc
) ab