I am migrating from MySQL to SQL Server. The following is a query that I had in MySQL, but I am not sure how to get variables working in SQL Server.
SET @listid = 0;
SELECT
list_id, last_updated, price, daily_return,
@cumu_ret := ROUND(
IF (@listid = list_id,
IF(daily_return IS NULL, 1.0, @cumu_ret * (1 + daily_return)),
IF(daily_return IS NULL, 1.0, last_cumulative_return * (1 + daily_return))), 10) AS cumulative_return,
@listid := list_id AS set_id
FROM
daily_return
ORDER BY
list_id, last_updated
SQL Server has a SUM() OVER
function, but ideally I need PRODUCT() OVER
.
Any ideas what I can do?
EDIT: when I try the following query...
DECLARE @listid int = 0;
DECLARE @cumu_ret decimal(24,10) = NULL;
SELECT
list_id, last_updated, price, daily_return,
@cumu_ret = ROUND(
IIF (@listid = list_id,
IIF(daily_return IS NULL, 1.0, @cumu_ret * (1 + daily_return)),
IIF(daily_return IS NULL, 1.0, last_cumulative_return * (1 + daily_return))), 10),
@listid = list_id
FROM
#daily_return
ORDER BY
list_id, last_updated
It throws an error:
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
EDIT 2: the
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
error is a symptom and not my main problem. I cannot use any of the suggested workarounds.
I am trying to get a PRODUCT() OVER
functionality going. Basically, in that column I am trying to get the previous cumulative return value and set the new row's value to previous cumulative * (1 + daily_return).