1 回答

TA貢獻2011條經(jīng)驗 獲得超2個贊
類似這樣的東西應該有效(您可以在子查詢中更改懲罰計算):
我假設只有當兩者都存在并且存在記錄時,才需要計算罰款。如果此假設不正確,并且您需要計算懲罰(如果存在任何一條或記錄),則可以從下面的查詢中刪除 。PRI
INT
PRI
INT
having count(*) = 2
select LOANNO, DUEDT, DUECD, STARTDT, ENDDT, DUEAMT, DUEPD
from
(
SELECT LOANNO, DUEDT, DUECD, STARTDT, ENDDT, DUEAMT, DUEPD
, case
when DUECD = 'PRI' then 1
when DUECD = 'INT' then 2
else 10
end ord
FROM LLDUEDET
WHERE DUEAMT != DUEPD
union all
select pc.LOANNO, pc.DUEDT, pc.DUECD, pc.STARTDT, pc.ENDDT, pc.DUEAMT, coalesce(pa.DUEPD, 0.0) DUEPD
,3 ord
from (
select LOANNO, DUEDT, 'PEN' DUECD, STARTDT, ENDDT, ((SUM(DUEAMT)-SUM(DUEPD)) * 30) * .1 DUEAMT
from LLDUEDET
WHERE DUEAMT != DUEPD
and DUECD in ('PRI', 'INT')
group by LOANNO, DUEDT, STARTDT, ENDDT
having count(*) = 2
) pc
left join (
select LOANNO, DUEDT, SUM(DUEPD) DUEPD
from LLDUEDET
where DUECD = 'PEN'
group by LOANNO, DUEDT
) pa on pa.loanno = pc.loanno and pa.duedt = pc.duedt
) s
order by LOANNO, DUEDT, ord
- 1 回答
- 0 關注
- 169 瀏覽
添加回答
舉報