Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何从我的销售表中获取累积列?_Sql_Sql Server - Fatal编程技术网

Sql 如何从我的销售表中获取累积列?

Sql 如何从我的销售表中获取累积列?,sql,sql-server,Sql,Sql Server,我有下面的场景 pid和月份构成复合主键 pid month amount 1 1 10 2 2 15 1 2 20 1 3 10 3 3 4 2 3 6 现在,将与表一起生成的列如下所示 pid month amount sum 1 1 10 10 2 2 15 15 1 2 20 30 1 3 10 40 3

我有下面的场景

pid和月份构成复合主键

pid month amount
1    1     10
2    2     15
1    2     20
1    3     10
3    3     4
2    3     6
现在,将与表一起生成的列如下所示

pid month amount   sum
1    1     10      10
2    2     15      15
1    2     20      30
1    3     10      40
3    3     4       4
2    3     6       21

查询应该是什么?

您没有指定正在使用的SQL Server的版本,但您应该能够使用以下内容来获取任何版本的运行总数:

select t1.pid, 
  t1.month, 
  t1.amount,
  (select sum(t2.amount) 
   from yourtable t2
   where t1.pid = t2.pid
     and t2.month <= t1.month) total
from yourtable t1;

请参见

您没有指定正在使用的SQL Server的版本,但您应该能够使用以下内容获取任何版本的运行总数:

select t1.pid, 
  t1.month, 
  t1.amount,
  (select sum(t2.amount) 
   from yourtable t2
   where t1.pid = t2.pid
     and t2.month <= t1.month) total
from yourtable t1;

如果使用SQL Server 2012,请参见:

SELECT *,SUM(amount) OVER(PARTITION BY pid ORDER BY month ROWS UNBOUNDED PRECEDING)'Total'
FROM YourTable

如果使用SQL Server 2012:

SELECT *,SUM(amount) OVER(PARTITION BY pid ORDER BY month ROWS UNBOUNDED PRECEDING)'Total'
FROM YourTable

此查询将实现以下功能:

SELECT t1.*, sum(t2.amount)
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.pid = t2.pid AND t1.month >= t2.month
GROUP BY t1.pid, t1.month, t1.amount

请参阅SQLFIDDLE:

此查询将实现以下功能:

SELECT t1.*, sum(t2.amount)
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.pid = t2.pid AND t1.month >= t2.month
GROUP BY t1.pid, t1.month, t1.amount

请参阅SQLFIDDLE:

什么版本的SQL Server?欢迎使用堆栈溢出。。。。到目前为止你试过什么?我们不是一个经常性的论坛,你问一个一般性的问题,我们都给你答案。然而,这里有很多人愿意帮助那些至少尝试过答案的人。什么版本的SQL Server?欢迎来到stack overflow。。。。到目前为止你试过什么?我们不是一个经常性的论坛,你问一个一般性的问题,我们都给你答案。然而,这里的许多人都愿意帮助那些至少尝试过答案的人。它不起作用,它获取了所有的空值column@user2718853使用您的示例数据集,它正在工作-请参阅此演示-嘿,蓝脚怪,我的错误,是的,它工作正常,感谢lotIt不工作,它获取了总数中的所有空值column@user2718853用你的示例数据集,它正在工作-看这个演示-嘿,蓝脚我的错误是的,它工作正常,谢谢!非常感谢Fabiencan我使用递归cte?工作!非常感谢Fabiencan我使用了递归cte?谢谢,但我是在sql server 2008上工作的谢谢,但我是在sql server 2008上工作的