上游給了一個明細表和一個總金額,明細表欄位為(產品號,公司號,金額),要求對明細金額按照產品號和公司號進行分組求和,也就是同一個產品號下各個公司的總金額,上游給的明細資料金額欄位為小數點后8位精度,所有金額加起來剛好是一個整數(總金額),但是我們的程式為將查詢出來的資料直接插入另外一張表b,這個表b的金額欄位為2位精度,這樣通過四舍五入插入到表b后就會產生尾差,也就是表b中的金額總和不等于總金額,請問各位大佬如何解決,我的想法是對查詢出來的結果現四舍五入然后與明細表中同一個產品號的總金額進行二次判斷,如果同一個產品號下金額一致則不做處理,如果相差1分或2分錢,就直接在查詢結果上加上或減去這個尾差,但是現在不知道如何對查詢出來的結果進行二次處理,各位大佬麻煩指導一下
uj5u.com熱心網友回復:
貼表結構、測驗資料、預期結果。uj5u.com熱心網友回復:
--A 明細表-產品公司維度 B匯總表-產品維度merge into A
USING (select rowid AS RD,
A.product,
A.company,
A.balance,
row_number() over(partition by A.product order by A.balance desc) as rn,--按金額排序,產品維度分組,取最大的金額對應的列
(sum(A.balance) over(partition by A.product) - b.tot_balance) as minus_balance --金額差額
from A, B
where A.product = B.product) T
ON (a.product = t.product and t.rn = 1 and a.rowid = t.rd and t.company = a.company)
when matched then
update set a.balance = a.balance - t.minus_balance --兜底
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/26375.html
標籤:基礎和管理
