Abstract: I am trying to rank these stocks factors by top quintile and bottom quintile to build a long/short portfolio..
Here is the table:
I have been trying this code:
DECLARE @CDate Date SET @CDate = '2006-12-30' -- dates as string must be yyyy-mm-dd WHILE @CDate < '2016-12-30' BEGIN With cte as( select *, NTILE(5) OVER (order by @CDate desc) as ntile from MOMENTUM_Quintile ) update cte set @CDate =ntile SET @CDate = DATEADD(mm, 1, @CDate) WHILE Month(@CDate) = Month(DATEADD(dd, 1, @CDate)) BEGIN SET @CDate = DATEADD(dd, 1, @CDate) END END
To reach this result
But it just does not gives me what I want.. looking for some insight for this