Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update values from a table using a NTILE function?

Abstract: I am trying to rank these stocks factors by top quintile and bottom quintile to build a long/short portfolio.

Table:

enter image description here

Desired result:

enter image description here

Code used:

UPDATE MOMENTUM_Quintile 
SET [2006-12-30] = NTILE(5) OVER (ORDER BY [2006-12-30] DESC)
FROM MOMENTUM_Quintile

Error message:

Msg 4108, Level 15, State 1, Line 7
Windowed functions can only appear in the SELECT or ORDER BY clauses.

like image 298
Robin_Hcp Avatar asked Apr 24 '26 19:04

Robin_Hcp


1 Answers

The error is clear,reason why you can't do this,has to do with Logical query processing .you can try below

 With cte
 as(
 select *, NTILE(5) OVER (order by [2006-12-30] desc) as ntile
from
MOMENTUM_Quintile
)
update cte 
set [2006-12-30]=ntile
like image 174
TheGameiswar Avatar answered Apr 26 '26 08:04

TheGameiswar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!