付款分配oracle sql查询 [英] payment distrubution oracle sql query
问题描述
我正在寻找一个查询,其中根据item_order将$ 1100分配给以下每个发票.同样,如果partial_payment_allowed设置为"N",则仅当分配的金额大于invoice_amt时才发生上述金额的分配,否则应跳过该行并继续下一个发票.
I am looking for a query where $1100 gets distributed to each invoice below based on the item_order. Also, if partial_payment_allowed is set to 'N' then distribution of the above amount should only happen if the distributed amount is greater than the invoice_amt else it should skip the row and carry on to next invoice.
Item_order inv_amount Partial_pmt_allowed
1 1256 N
2 1134 N
3 800 N
4 200 Y
5 156 Y
因此,如果我们通过$ 1100,查询的最终结果将是
So, the final result of the query if we pass $1100 would be
Item_order inv_amount Partial_pmt_allowed Dist_amount Balance_amt
1 1256 N 0 1100
2 1134 N 0 1100
3 800 N 800 300
4 200 Y 200 100
5 156 Y 100 0
我们正在努力避免循环,任何评论都将受到高度赞赏.谢谢
We are trying to avoid loops, any comments are highly appreciated.Thank you
推荐答案
这是SQL MODEL
子句的好用例.
This is a good use case for the SQL MODEL
clause.
-- Set up test data (since I don't have your table)
with inv_raw (item_order, inv_amount, partial_pmt_allowed) as (
SELECT 1, 1256, 'N' FROM DUAL UNION ALL
SELECT 2, 1134, 'N' FROM DUAL UNION ALL
SELECT 3, 800, 'N' FROM DUAL UNION ALL
SELECT 4, 200, 'Y' FROM DUAL UNION ALL
SELECT 5, 156, 'Y' FROM DUAL),
-- Ensure that the column we are ordering by is densely populated
inv_dense (dense_item_order, item_order, inv_amount, partial_pmt_allowed) as
( SELECT dense_rank() OVER ( PARTITION BY NULL ORDER BY item_order ), item_order, inv_amount, partial_pmt_allowed FROM inv_raw ),
-- Give us a way to input the payment amount
param as ( SELECT 1100 p_payment_amount FROM DUAL )
-- The actual query starts here
SELECT item_order,
inv_amount,
partial_pmt_allowed,
--remaining_in,
applied dist_amount,
remaining_out balance_amt
FROM param, inv_dense
MODEL
DIMENSION BY ( dense_item_order )
MEASURES ( p_payment_amount, item_order, inv_amount, partial_pmt_allowed, 0 applied, 0 remaining_in, 0 remaining_out )
RULES AUTOMATIC ORDER (
-- The amount carried into the first row is the payment amount
remaining_in[1] = p_payment_amount[1],
-- The amount carried into subsequent rows is the amount we carried out of the prior row
remaining_in[dense_item_order > 1] = remaining_out[CV()-1],
-- The amount applied depends on whether the amount remaining can cover the invoice
-- and whether partial payments are allowed
applied[ANY] = CASE WHEN remaining_in[CV()] >= inv_amount[CV()] OR partial_pmt_allowed[CV()] = 'Y' THEN LEAST(inv_amount[CV()], remaining_in[CV()]) ELSE 0 END,
-- The amount we carry out is the amount we brought in minus what we applied
remaining_out[ANY] = remaining_in[CV()] - applied[CV()]
)
ORDER BY item_order;
结果
ITEM_ORDER |INV_AMOUNT |PARTIAL_PMT_ALLOWED |DIST_AMOUNT |BALANCE_AMT |
-----------|-----------|--------------------|------------|------------|
1 |1256 |N |0 |1100 |
2 |1134 |N |0 |1100 |
3 |800 |N |800 |300 |
4 |200 |Y |200 |100 |
5 |156 |Y |100 |0 |
这篇关于付款分配oracle sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!