sql >> Database teknologi >  >> RDS >> Sqlserver

SQL Server Tildeling af beløb i regningen

WITH cl AS (select o.LINEID, o.BILL, o.[Total Amount], (select SUM([Total Amount]) from bills t where o.BILL = t.BILL and o.LINEID >= t.LINEID) as 'sum_total_ammount' from bills o inner join payment p on o.BILL = p.BILL) select o.LINEID, o.BILL, o.[Total Amount], case when p.[Paid Amount] >= sum_total_ammount then o.[Total Amount] else (o.[Total Amount] - sum_total_ammount + p.[Paid Amount]) end as 'Allocated Amount' from cl o inner join payment p on o.BILL = p.BILL and (o.[Total Amount] - sum_total_ammount + p.[Paid Amount]) > 0

Her er en demo på SqlFiddle .




  1. ExecuteReader kræver en åben og tilgængelig forbindelse. Forbindelsens aktuelle tilstand er Forbinder

  2. Entity Framework Code Først med SQL Server-synonymer

  3. MySQL LIKE + php sprintf

  4. MySQLi - erklærer variabel efter bind_param?