我将在递归查询期间,并且需要存储一些关联数据的符号。怎么做到最好?
也许
IF(sign, 1, -1)
tinyint
并将签名值限制为+1或-1。如果将值限制为+1和-1,最好如何做到这一点?有趣的是,整数显然不能存储在enum
数据类型中(但是,1和-1可以是字符串)
我的递归查询类似于以下内容,但weight
将更改为sign
(参考https://stackoverflow.com/a/45258770/1032531)。
WITH
RECURSIVE t AS(
SELECT
pha.*,
h.value
FROM
portfolios_has_accounts pha
LEFT OUTER JOIN holdings h ON
h.accounts_id = pha.accounts_id
WHERE
pha.portfolios_id = "p1"
UNION ALL
SELECT
pha.portfolios_id,
pha.accounts_id,
pha.weight * t.weight,
h.value
FROM
t
INNER JOIN portfolios_has_accounts pha ON
pha.portfolios_id = t.accounts_id
LEFT OUTER JOIN holdings h ON
h.accounts_id = pha.accounts_id
)
SELECT
SUM(weight * value)
FROM
t
WHERE
value IS NOT NULL;