1

我有一个主要的表:

(  
time date,  
qty int  
)  

我想创建一个查询,所以每天我都会得到当天和之前所有天的数量总和,所以对于这个数据

-----------------------    
time        | qty    
01/09/2009  | 3   
02/09/2009  | 8  
03/09/2009  | 2 
04/09/2009  | 5 

我得到:

-----------------------    
time        | total    
01/09/2009  | 3   
02/09/2009  | 11  
03/09/2009  | 13 
04/09/2009  | 18 

提前致谢

4

3 回答 3

0
SELECT TIME, (SELECT SUM(QTY) FROM main m2 WHERE m2.ITME <= mt1.TIME) AS sum  
FROM main m1  
ORDER BY TIME  

这应该可以解决问题,尽管它可能不是最快的解决方案。

于 2013-07-12T07:42:46.303 回答
0
SELECT
  M.Time,
  SUM(M2.Qty) RunningTotal
FROM
  Main M
  LEFT JOIN Main M2 ON M.Time >= M2.Time
GROUP BY
  M.Time;
于 2013-07-12T07:54:28.940 回答
0

这应该会给你一个更快的结果

SELECT time
     , @tot_qty := @tot_qty+qty AS tot_qty
FROM Table1 
JOIN (SELECT @tot_qty := 0) d
order by time

SQL 小提琴

于 2013-07-12T08:11:39.597 回答