简体   繁体   English

如何将一列的第一条记录与另一列的第二条记录求和?

[英]How to Sum the 1st record of one column with the 2nd record of another column?

I am trying the Sum the 2nd record of one column with the 1st record of another column and store the result in a new column 我正在尝试将一列的第二条记录与另一列的第一条记录求和并将结果存储在新列中

Here is the example SQL Server table 这是示例SQL Server表

Emp_Code   Emp_Name    Month          Opening_Balance   
 G101      Sam          1              1000              
 G102      James        2              -2500             
 G103      David        3              3000     
 G104      Paul         4              1800     
 G105      Tom          5              -1500      

I am trying to get the output as below on the new Reserve column 我正在尝试在新的Reserve列上获得如下输出

Emp_Code   Emp_Name    Month          Opening_Balance    Reserve
 G101      Sam          1              1000              1000       
 G102      James        2              -2500             -1500         
 G103      David        3              3000               1500
 G104      Paul         4              1800               3300
 G105      Tom          5              -1500              1800

Actually the rule for calculating the Reserve column is that 实际上,计算“ Reserve列的规则是

  1. For Month-1 it's the same as Opening Balance Month-1Opening Balance相同
  2. For rest of the months its Reserve for Month-2 = Reserve for Month-1 + Opening Balance for Month-2 在接下来的几个月中,其第二个月的Reserve for Month-2 =第Reserve for Month-1 +第Reserve for Month-1 Opening Balance for Month-2

You seem to want a cumulative sum. 您似乎想要一个累加的总和。 In SQL Server 2012+, you would do: 在SQL Server 2012+中,您可以执行以下操作:

select t.*,
       sum(opening_balance) over (order by [Month]) as Reserve
from t;

In earlier versions, you would do this with a correlated subquery or apply : 在早期版本中,您可以使用相关子查询来执行此操作,也可以apply

select t.*,
       (select sum(t2.opening_balance) from t t2 where t2.[Month] <= t.[Month]) as reserve
from t;

You can do a self join. 您可以进行自我加入。

SELECT t.Emp_Code, t.Emp_Name, t.Month, t.Opening_Balance, t.Opening_Balance + n.Reserve
FROM Table1 t
JOIN Table2 n
ON t.Month = n.Month - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在记录X之前选择第一和第二记录 - Select 1st and 2nd Record before record X 如何在sql中比较同一表的第一条记录中的列A和第二条记录中的列B - How to compare columnA in 1st record and columnB in 2nd record of a same table in sql MySQL-用第二行的第二列减去第一行的第一列 - MySQL - Subtract 1st column of 1st row with 2nd column of 2nd row SQL:两列计算第一列和第二列的第三列总和 - SQL : Two Columns Count Third Column Sum of 1st and 2nd Column 仅当第 2 个表中不存在记录时,才将第 1 个表中的记录插入到第 2 个表中 - Insert records from 1st table to 2nd table only when the record is not present in the 2nd table 如何将现有列的第一行填充到第二行的新列中? - How to populate 1st row of existing column into new column of 2nd row? 根据第一个表更新第二个表列 - update 2nd table column based on 1st table 如何仅返回 1 列的第二行记录 - how to return 2nd row record for only 1 column SQL查询要在表上进行迭代,以计算在其字段之一中具有相同值的第一条记录和第二条记录之间的时间差? - SQL query to iterate on a table to calculate the time difference between the 1st and 2nd record that have the same value in one of their fields? Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列? - Mysql - How to display column from 2nd table based on values from 2 columns of 1st table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM