MySQL 8 - 表达式中的用户变量已弃用(复杂计算)
MySQL 8 - user variables within expressions is deprecated (complex calculation)
// 编辑 //
下面是原始问题,但是我的问题真的可以用一个问题来简化...我怎样才能在不使用用户变量的情况下得到下面的输出?!
SELECT ID, @cumu_return:=IF(id = 1, 1, @cumu_return + (@cumu_return * ret)) AS cumulative_return
FROM (
SELECT 1 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 2 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 3 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 4 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 5 AS ID, 1 AS num, 0.1 AS ret
) t
// 结束编辑 //
我有一个table如下...
CREATE TABLE `daily_return` (
`id` int(11) NOT NULL,
`list_id` int(11) NOT NULL,
`last_updated` datetime NOT NULL DEFAULT '2000-01-01 00:00:00',
`daily_return` float NOT NULL,
`last_return` float NOT NULL, KEY (`id`)
)
'daily return' 是 list_id 的价格百分比变化,每天计算,last_return 是 first/starting return 数字。
我需要使用以下逻辑计算累积 return 指标:
'Previous Day Return' + ('Previous Day Return' * 'Daily Return')
因此,我有以下查询...
INSERT INTO cmc_cumulative_return (list_id, last_updated, cumulative_return)
SELECT list_id, last_updated, cumulative_return FROM (
SELECT id, list_id, last_updated, daily_return, last_return,
@cumu_return:=IF(id = 1, last_return, @cumu_return + (@cumu_return * daily_return)) AS cumulative_return
FROM daily_return c
) t WHERE id <> 1;
当我在程序中 运行 this 时,它会抛出警告:
Setting user variables within expressions is deprecated and will be
removed in a future release. Please set variables in separate
statements instead.
我在Whosebug上看到过一些其他类似的问题,但都是简单的增量计算,可以用ROW_NUMBER() OVER或SUM() OVER代替,但我无法弄清楚如何删除上面查询中的变量。
最初,我使用了 3-4 个变量,但现在我已经减少到一个,请求您帮助删除最后一个。
编辑 2
查询实际数据:(包括 Gordon 的计算)
SELECT id, list_id, last_updated, daily_return, last_return,
@cumu_return:=IF(id = 1, last_return, @cumu_return + (@cumu_return * daily_return)) AS cumulative_return,
(
EXP(SUM(LN(1 + daily_return)) OVER (ORDER BY id)) / (1 + daily_return)
) as cumulative_return2
FROM (
SELECT 1 AS id, 2 AS list_id, '2019-02-20' AS last_updated, 0 AS daily_return, 1.15 AS last_return UNION ALL
SELECT 2 AS id, 2 AS list_id, '2019-02-21' AS last_updated, 0.0145999858 AS daily_return, 1.15 AS last_return UNION ALL
SELECT 3 AS id, 2 AS list_id, '2019-02-22' AS last_updated, -0.0503679203 AS daily_return, 1.15 AS last_return UNION ALL
SELECT 4 AS id, 2 AS list_id, '2019-02-23' AS last_updated, 0.0111594238 AS daily_return, 1.15 AS last_return
) t
您可以使用自然对数和求幂来计算累积积。
INSERT INTO cmc_cumulative_return (list_id, last_updated, cumulative_return)
SELECT list_id, last_updated,
(MAX(CASE WHEN id = 1 THEN last_return END) OVER () *
EXP(SUM(LN(1 + daily_return)) OVER (ORDER BY id)
) / (1 + daily_return)
) as cumulative_return
FROM daily_return c;
如果您真的想排除 id = 1 的位置,则需要额外的子查询级别。
Here 是一个 db<>fiddle.
// 编辑 //
下面是原始问题,但是我的问题真的可以用一个问题来简化...我怎样才能在不使用用户变量的情况下得到下面的输出?!
SELECT ID, @cumu_return:=IF(id = 1, 1, @cumu_return + (@cumu_return * ret)) AS cumulative_return
FROM (
SELECT 1 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 2 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 3 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 4 AS ID, 1 AS num, 0.1 AS ret UNION ALL
SELECT 5 AS ID, 1 AS num, 0.1 AS ret
) t
// 结束编辑 //
我有一个table如下...
CREATE TABLE `daily_return` (
`id` int(11) NOT NULL,
`list_id` int(11) NOT NULL,
`last_updated` datetime NOT NULL DEFAULT '2000-01-01 00:00:00',
`daily_return` float NOT NULL,
`last_return` float NOT NULL, KEY (`id`)
)
'daily return' 是 list_id 的价格百分比变化,每天计算,last_return 是 first/starting return 数字。
我需要使用以下逻辑计算累积 return 指标:
'Previous Day Return' + ('Previous Day Return' * 'Daily Return')
因此,我有以下查询...
INSERT INTO cmc_cumulative_return (list_id, last_updated, cumulative_return)
SELECT list_id, last_updated, cumulative_return FROM (
SELECT id, list_id, last_updated, daily_return, last_return,
@cumu_return:=IF(id = 1, last_return, @cumu_return + (@cumu_return * daily_return)) AS cumulative_return
FROM daily_return c
) t WHERE id <> 1;
当我在程序中 运行 this 时,它会抛出警告:
Setting user variables within expressions is deprecated and will be removed in a future release. Please set variables in separate statements instead.
我在Whosebug上看到过一些其他类似的问题,但都是简单的增量计算,可以用ROW_NUMBER() OVER或SUM() OVER代替,但我无法弄清楚如何删除上面查询中的变量。
最初,我使用了 3-4 个变量,但现在我已经减少到一个,请求您帮助删除最后一个。
编辑 2
查询实际数据:(包括 Gordon 的计算)
SELECT id, list_id, last_updated, daily_return, last_return,
@cumu_return:=IF(id = 1, last_return, @cumu_return + (@cumu_return * daily_return)) AS cumulative_return,
(
EXP(SUM(LN(1 + daily_return)) OVER (ORDER BY id)) / (1 + daily_return)
) as cumulative_return2
FROM (
SELECT 1 AS id, 2 AS list_id, '2019-02-20' AS last_updated, 0 AS daily_return, 1.15 AS last_return UNION ALL
SELECT 2 AS id, 2 AS list_id, '2019-02-21' AS last_updated, 0.0145999858 AS daily_return, 1.15 AS last_return UNION ALL
SELECT 3 AS id, 2 AS list_id, '2019-02-22' AS last_updated, -0.0503679203 AS daily_return, 1.15 AS last_return UNION ALL
SELECT 4 AS id, 2 AS list_id, '2019-02-23' AS last_updated, 0.0111594238 AS daily_return, 1.15 AS last_return
) t
您可以使用自然对数和求幂来计算累积积。
INSERT INTO cmc_cumulative_return (list_id, last_updated, cumulative_return)
SELECT list_id, last_updated,
(MAX(CASE WHEN id = 1 THEN last_return END) OVER () *
EXP(SUM(LN(1 + daily_return)) OVER (ORDER BY id)
) / (1 + daily_return)
) as cumulative_return
FROM daily_return c;
如果您真的想排除 id = 1 的位置,则需要额外的子查询级别。
Here 是一个 db<>fiddle.