sql >> データベース >  >> RDS >> Sqlserver

SQL Server 請求額の割り当て

    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
      

    SqlFiddle のデモはこちら .




    1. MariaDBで時間、分、秒を時間値から分離する4つの方法

    2. Oracle:SQLクエリで自然な順序付けを実装するにはどうすればよいですか?

    3. MySQL For Visual Studio 2012/2013

    4. Oracle®SQL*Loaderを使用したSQLServerデータの挿入