Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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,请注意,我有两个表: 我希望trnamt from表格salary更新为rec记录中的tbl_emi,但金额不应超过每个记录的emi金额,例如,在这种情况下,salary表格中的金额1000应添加到第一个记录中500,再添加到tbl_emi表格的第二个记录中500 tbl_emi EMI DUEDT REC Acno 500 4/30/2014 500 123 500 5/30/2014 0

请注意,我有两个表:

我希望trnamt from表格salary更新为rec记录中的tbl_emi,但金额不应超过每个记录的emi金额,例如,在这种情况下,salary表格中的金额1000应添加到第一个记录中500,再添加到tbl_emi表格的第二个记录中500

               tbl_emi
EMI      DUEDT      REC     Acno
500    4/30/2014   500       123
500    5/30/2014   0         123 
500    6/30/2014   0         123 

              slary
         Acno      Trnamt
         123         1000
我使用了下面的查询,但它仍在tbl_emi tbale+工资表中的trnamt中添加rec字段,但不应超过emi金额

选择查询1:为累计emi创建临时表

select a.emi, a.duedt, a.rec, a.acno, coalesce(sum(b.emi),0) as emi_accumulated
into #accumulated_amount5
from tbl_emi a
left join tbl_emi b on a.acno = b.acno and b.duedt < a.duedt
group by a.emi, a.duedt, a.rec, a.acno;
更新查询1:更新rec金额

update #accumulated_amount_with_salary5
set rec = rec + case
    when salary_amt < emi_accumulated then 0
    when (salary_amt - emi_accumulated) < emi then salary_amt - emi_accumulated
    else emi
end

您可以在薪资表上使用插入后触发器,该触发器将在每次插入薪资表后更新tbl_emi表,
有关如何在sql server中创建触发器的参考,请参阅

假设您有累积和函数,通过计算累积emi值并与trnamt进行比较,这相对容易:

即使没有累积和,您仍然可以使用相关子查询执行相同的操作

编辑:

在SQL Server 2008中,可以将累积总和表示为:

with toupdate as (
      select e.*,
             (select sum(e2.emi)
              from tbl_emi e2
              where e2.acno = e.acno and
                    e2.duedt <= e.duedt
             )  as cumemi
      from tbl_emi e
     )
update toupdate
    set rec = (case when trnamt > cumemi then emi
                    else trnamt - cumemi + emi
               end)
    from toupdate join
         slary
         on toupdate.acno = slary.acno and
            toupdate.cumemi - emi < trnamt;

这在sql server 2008上有效吗?因为它向我显示了错误:“order”附近的语法不正确。您是如何创建topdate对象的?@user2786306。那是个打字错误。它应该是最新的。
update t
set lastrecdate=(CAST(GETDATE() AS DATE)), rec = a.rec
from tbl_emi t
    inner join #accumulated_amount_with_salary5 a on t.acno = a.acno and t.duedt = a.duedt
    --Finance
with toupdate as (
      select e.*, sum(emi) over (partition by acno order by duedt) as cumemi
      from tbl_emi e
     )
update toupdate
    set rec = (case when trnamt > cumemi then emi
                    else trnamt - cumemi + emi
               end)
    from toupdate join
         slary
         on toupdate.acno = slary.acno and
            toupdate.cumemi - emi < trnamt;
with toupdate as (
      select e.*,
             (select sum(e2.emi)
              from tbl_emi e2
              where e2.acno = e.acno and
                    e2.duedt <= e.duedt
             )  as cumemi
      from tbl_emi e
     )
update toupdate
    set rec = (case when trnamt > cumemi then emi
                    else trnamt - cumemi + emi
               end)
    from toupdate join
         slary
         on toupdate.acno = slary.acno and
            toupdate.cumemi - emi < trnamt;