Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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_Sql Server 2014 - Fatal编程技术网

在sql中优先向债权人分享现金

在sql中优先向债权人分享现金,sql,sql-server,sql-server-2014,Sql,Sql Server,Sql Server 2014,我在sql 2014中有一个名为“tblPaymentPlan”的表,如下所示: Creditors PlanToPay َAmount ---------------------------------- A 2017-01-20 2000 A 2017-02-20 1500 A 2017-03-20 3000 B 2017-01-25 3000 B

我在sql 2014中有一个名为“tblPaymentPlan”的表,如下所示:

Creditors    PlanToPay      َAmount
----------------------------------
A            2017-01-20     2000
A            2017-02-20     1500
A            2017-03-20     3000
B            2017-01-25     3000
B            2017-02-25     1000
还有另一个名为“tblPaid”的表,如下所示:

Creditors    Paid      َ
-----------------
A            4500
B            3500
我期望的结果是:

Creditors    PlanToPay      َRemain
----------------------------------
A            2017-01-20     0
A            2017-02-20     0
A            2017-03-20     2000
B            2017-01-25     0
B            2017-02-25     500
我根本不知道该怎么做这项工作!请你帮我做这项工作好吗。请告知,我的表格中有很多记录。
我需要这个查询用于预算规划。(我们可以使用数字来定义优先级,而不是日期)

您可以从支付表中的金额中减去总金额,如果该金额小于0,则将“保留”设置为0,否则为总金额与总金额的差额

select pp.creditors,pp.plantopay,
case when sum(pp.amount) over(partition by pp.creditors order by pp.plantopay)-coalesce(pd.paid,0) <= 0 then 0
else sum(pp.amount) over(partition by pp.creditors order by pp.plantopay)-coalesce(pd.paid,0) end as remain
from tblpaymentplan pp
left join tblPaid pd on pp.creditors=pd.creditors
选择pp.benders,pp.plantopay,

当总和(pp.amount)超过(按pp.债权人分割,按pp.plantopay排序)-合并(pd.paid,0)时,您需要的是所欠金额的连续总计,从中您可以减去已支付的金额

SELECT Creditors, PlanToPay, IIF(ABS(Remain)!=Remain,0,IIF(Remain<Amount,Remain,Amount)) as Remain
FROM (SELECT pp.Creditors, pp.PlanToPay, pp.Amount,
  SUM(pp.Amount) OVER(PARTITION BY pp.Creditors ORDER BY pp.PlanToPay ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)-tp.paid AS Remain
  FROM tblPaymentPlan pp 
  JOIN (SELECT creditors, sum(paid) as paid from tblpaid group by creditors) tp
  ON pp.creditors = tp.creditors) ss
ORDER By Creditors, PlanToPay
选择债权人,PlanToPay,IIF(ABS(剩余)!=剩余,0,IIF(剩余

在窗口功能(
SUM OVER
)中,
分区将债权人分开,
顺序决定行的排列方式(按日期),并且
ROWS
子句告诉它使用该行之前分区中的所有行,并将该行包括在运行总计中。然后,我们从该运行总计中减去支付给该债权人的所有款项的总和

select pp.creditors,pp.plantopay,
case when sum(pp.amount) over(partition by pp.creditors order by pp.plantopay)-coalesce(pd.paid,0) <= 0 then 0
else sum(pp.amount) over(partition by pp.creditors order by pp.plantopay)-coalesce(pd.paid,0) end as remain
from tblpaymentplan pp
left join tblPaid pd on pp.creditors=pd.creditors
这当然给了我们很多负数,所以我们在子查询中执行。主查询检查剩余值的绝对值是否等于该值,如果为正,则为true,如果为非,则为false,如果为真,则返回剩余值,否则返回0


更新-添加了对仍欠值的多行的处理

此处稍有偏差,我将避免使用此解决方案,因为它会对某些行执行两次加窗求和。首先计算案例,然后在案例导致更多欠值时再次计算。这比其他两个解决方案效率低,后者只计算一次加窗求和en多次使用该结果。