Oracle
 sql >> डेटाबेस >  >> RDS >> Oracle

एसक्यूएल भुगतान वितरण

इस प्रश्न के उत्तर का विस्तार करते हुए:Payment distrubution oracle sql query

आप अब भी SQL MODEL का उपयोग कर सकते हैं खंड। इस संस्करण में, आपको प्रत्येक विशिष्ट account_num . के लिए अलग-अलग गणनाओं की आवश्यकता है . आप इसे PARTITION . का उपयोग करके प्राप्त कर सकते हैं SQL का कीवर्ड MODEL account_num . द्वारा विभाजन का खंड ।

इस तरह (चरण-दर-चरण स्पष्टीकरण के लिए SQL टिप्पणियाँ देखें):

-- Set up test data (since I don't have your table)
WITH inv_raw (item_order, inv_amount, partial_pmt_allowed, account_num, cr_amt) AS (
SELECT 1, 1256, 'Y', 12, 1000 FROM DUAL UNION ALL
SELECT 2, 1134, 'Y', 12, 1000 FROM DUAL UNION ALL
SELECT 3, 800, 'Y', 13, 1200 FROM DUAL UNION ALL
SELECT 4, 200, 'N',13, 1200 FROM DUAL UNION ALL
SELECT 5, 156, 'N',13, 1200 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, account_num, cr_amt) AS
( SELECT DENSE_RANK() OVER ( PARTITION BY account_num ORDER BY item_order ), item_order, inv_amount, partial_pmt_allowed, account_num, cr_amt 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 
   account_num,
   item_order,
   inv_amount,
   partial_pmt_allowed,
   applied dist_amount,
   remaining_out balance_amt,
   cr_amt
FROM inv_dense
MODEL
-- We want a completely separate calculation for each distinct account_num
PARTITION BY ( account_num )
-- We'll output one row for each value of dense_item_order.
-- We made item_order "dense" so we can do things like CV()-1 to get the 
--   previous row's values.
DIMENSION BY ( dense_item_order )
MEASURES ( cr_amt, 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] = cr_amt[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 account_num, item_order;



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Oracle 11g का नाम बदलें। परमाणु होने की गारंटी?

  2. त्रुटि- ORA-00060:संसाधन की प्रतीक्षा करते समय गतिरोध का पता चला

  3. Oracle® . से SQL सर्वर संग्रहीत कार्यविधियाँ

  4. Oracle में न्यूलाइन कैरेक्टर कैसे दर्ज करें?

  5. नामांकित क्वेरी, मूल क्वेरी या मानदंड API