我在SlamData中使用了查询功能。 我的代码:
SELECT
DATE_PART("year",thedate) AS year,DATE_PART("month",thedate) AS month,
SUM(runningPnL) AS PnL
FROM "/Mickey/testdb/sampledata3" AS c
GROUP BY DATE_PART("year", thedate) ,DATE_PART("month", thedate)
order by DATE_PART("year", thedate) ,DATE_PART("month", thedate)
我桌子的摘录:
PnL month year
-1651.8752 1 2001
17180.4776 2 2001
48207.54560000001 3 2001
现在,我怎样才能找到PnL的累积总和?
例如。-1651.8752
第一个月
15528.6024
第二个月
非常感谢>。<
答案 0 :(得分:0)
我正在为累积总和生成与您相同的样本数据。希望你能得到一些想法。
Create table tempData
(
pnl float,
[month] int,
[year] int
)
Go
insert into tempData values ( -1651.8752, 1,2001)
insert into tempData values ( 17180.4776, 2,2001)
insert into tempData values ( 48207.54560000001, 3,2001)
Select * , (SELECT SUM(Alias.pnl)
FROM tempData As Alias
WHERE Alias.[Month] <= tempData.[Month]
) As CumulativSUM
FROm tempData
ORDER BY tempData.[MOnth]
答案 1 :(得分:0)
完成 我的代码是
SELECT a1.year, a1.month, a1.PnL, a1.PnL/(SUM(a2.PnL)+125000) as Running_Total
FROM
/米奇/ TESTDB / sampledata6 as a1,
/米奇/ TESTDB / sampledata6 as a2
WHERE (a1.month > a2.month And a1.year=a2.year) or (a1.year>a2.year)
GROUP BY a1.year, a1.month,a1.PnL
ORDER BY a1.year,a1.month ASC;