我有两个包含客户信息的表:
Portfolios
accountId Date portfolio
Barney 2013-06-28 Slate
Barney 2013-07-01 Bedrock
Barney 2013-07-02 Bedrock
Fred 2013-06-28 Slate
Fred 2013-07-01 Slate
Fred 2013-07-02 Slate
Money
accountId Date value
Barney 2013-06-28 $100.00
Barney 2013-07-01 $101.00
Barney 2013-07-02 $103.00
Fred 2013-06-28 $500.00
Fred 2013-07-01 $503.00
Fred 2013-07-02 $498.00
我想按日期总结每种策略的投资组合价值 - Slate和Bedrock。我可以比较两者并获得特定策略的运行值。例如:
SELECT Portfolios.accountId, Portfolios.Date, Portfolios.strategy,
Money.accountId, Money.Date, Money.strategy, SUM(Money.value) as sumtotal
from Money, Portfolios
WHERE Money.accountId = Portfolios.accountId
AND Money.Date = Portfolios.Date
AND Portfolio.portfolio = 'Slate'
GROUP BY Date ORDER by Date ASC
这很有效。
Date value
2013-06-28 $600.00
2013-07-01 $503.00
2013-07-02 $498.00
我无法工作的是比较不同日期的策略:
SUM (Money.value) WHERE Portfolios.strategy.date != Portfolios.strategy.date (-1)
GROUP BY Date
这样查询只会返回$ 100(Barney投资组合在2013-06-28之前的最后一个值,然后才转换策略。