1

我在 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第二个月
非常感谢>.<

4

2 回答 2

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]
于 2016-06-29T08:58:32.647 回答
0

完成我的代码是

SELECT a1.year, a1.month, a1.PnL, a1.PnL/(SUM(a2.PnL)+125000) as Running_Total 

FROM/Mickey/testdb/sampledata6 as a1,/Mickey/testdb/sampledata6as 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;

于 2016-06-30T02:08:40.977 回答