2016-11-09 22 views
3

我需要一種替代方法來簡化此代碼。由於多個連接,SQL Server錯誤簡化了代碼

此代碼根據貸款還款計劃計算客戶餘額的年齡。該過濾器是1-7天,8-30,31-60 ....等等,直到其達到181以上

with membersWithLoans as -- gets members with loan 
(
    select 
     a.memberid, a.loanid, a.loanamt, a.intamt 
    from 
     loanmst a 
    where 
     loandt <= '12/19/2016' 
     and status = 'O' 
) 
,selectPaymentToDate as -- gets payments of the members to date 
(
    select 
     b.loanid, sum(a.princollamt) as princollamt1, 
     sum(a.intcollamt) as intcollamt1 
    from 
     collectiondtl a 
    inner join 
     membersWithLoans b on a.loanid = b.loanid 
    where 
     a.accdate <= '12/19/2016' 
    group by 
     b.loanid) 
,selectBalanceToDate as -- gets the balance of member to date 
(
    select 
     b.loanid, 
     sum(a.princeamt) as prinBalanceToDate, 
     sum(a.instamt) as intBalanceToDate, 
     sum(a.insamt) as insuBalanceToDate 
    from 
     loandtl a 
    inner join 
     membersWithLoans b on a.loanid = b.loanid 
    where 
     a.duedt <= '12/19/2016' 
    group by 
     b.loanid) 
, combineBalanceWithpayment as -- combine payment and balance 
(
    select a.loanid,a.loanamt, a.intamt, 
(case 
when b.prinBalanceToDate is null then 0 
else b.prinBalanceToDate end) as prinBalanceToDate2, 
(case 
when b.intBalanceToDate is null then 0 
else b.intBalanceToDate end) as intBalanceToDate2, 
(case 
when b.insuBalanceToDate is null then 0 
else b.insuBalanceToDate end) as insuBalanceToDate2, 
(case 
when c.princollamt1 is null then 0 
else c.princollamt1 end) as PrincipalCollectiontoDate, 
(case 
when c.intcollamt1 is null then 0 
else c.intcollamt1 end) as IntCollectiontoDate, 

cast(((case 
when b.prinBalanceToDate is null then 0 
else b.prinBalanceToDate 
end) 
- 
(case 
when c.princollamt1 is null then 0 
else c.princollamt1 end))as decimal(10,2)) as Arrears 
from 
membersWithLoans a 
left join selectBalanceToDate b 
on a.loanid=b.loanid 
left join selectPaymentToDate c 
on a.loanid=c.loanid 
) 

,filterNegativeArrears as 
(
select * 
from 
combineBalanceWithpayment 
where Arrears > 0 
) 

上面的代碼獲取的會員信息

,select1To7days as -- this code gets amount to be paid in a specific schedule 
(
select b.loanid, 
    sum((case 
    when a.princeamt is null then 0 
    else a.princeamt end))as prin7Daysbalance 

from loandtl a 
inner join membersWithLoans b 
on a.loanid=b.loanid 
where 
    a.duedt > DATEADD(day,-7,'12/19/2016') 
     and 
    a.duedt<='12/19/2016' 
group by b.loanid 
) 

,select8to30days as -- this code gets amount to be paid in a specific schedule 
(
select b.loanid, 
    sum((case 
    when a.princeamt is null then 0 
    else a.princeamt end))as prin8To30Daysbalance 
from loandtl a 
inner join membersWithLoans b 
on a.loanid=b.loanid 
where 
    a.duedt<=DATEADD(day,-7,'12/19/2016') 
    and a.duedt > DATEADD(day,-30,'12/19/2016') 
group by b.loanid 
) 

-- and so on ..... the filters for schedule is compose of 31 to 60days, 61 to 90 days, 
--121 to 180 days, 181 and above. there is no pattern since it the requirement on days may change 


, computePar1To7days as -- computes the 1 to 7 days 
(
select a.loanid, cast((a.arrears - a.Par1To7days) as decimal(10,2)) as deductedArrears, a.Par1To7days 
from 
(
    select a.loanid,a.arrears, 
    cast((case 
    when a.arrears >= b.prin7Daysbalance then b.prin7Daysbalance -- if the arrears is greater than the 7days balance to be collected then it will be show 
    else a.arrears end)as decimal(10,2))as Par1To7days -- else the remaining is the arrears 
    from 
    filterNegativeArrears a 
    left join select1To7days b 
    on a.loanid=b.loanid 
) a 
where cast((a.arrears - a.Par1To7days) as decimal(10,2)) > 0 
) 

,computePar8To30days as -- computes the 8 to 30 days 
(
select a.loanid, cast((a.arrears - a.Par8To30days)as decimal(10,2)) as deductedArrears, a.Par8To30days 
from 
(
    select a.loanid, a.deductedArrears as arrears, 
    cast((case 
    when (a.deductedArrears) > 0 
     then 
      (case 
       when (a.deductedArrears)>= b.prin8To30Daysbalance 
        then b.prin8To30Daysbalance 
       else (a.deductedArrears) 
      end) 
    else 0 end)as decimal(10,2))as Par8To30days 

    from computePar1To7days a 
    left join select8To30days b 
    on a.loanid=b.loanid 
) a 
where cast((a.arrears - a.Par8To30days) as decimal(10,2)) > 0 
) 
-- so on until all par is computed. 31 to 60 days, 61 to 90 days, 
--121 to 180 days, 181 and above. there is no pattern since it the requirement on days may change 

的以上代碼從特定調度數據的像 1至7天,8-30天,31至60天,61至90天,121至180天,181和上述

select a.*, 
b.Par1To7days, 
c.Par8To30days, 
d.Par31To60days, 
e.Par61To90days, 
f.Par91To120days, 
g.Par121To180days --, 
--h.Par181AndAbovedays 
from 

filterNegativeArrears a 
left join computePar1To7days b 
on a.loanid=b.loanid 
left join computePar8To30days c 
on a.loanid=c.loanid 
left join computePar31To60days d 
on a.loanid=d.loanid 
left join computePar61To90days e 
on a.loanid=e.loanid 
left join computePar91To120days f 
on a.loanid=f.loanid 
left join computePar121To180days g 
on a.loanid=g.loanid 
--left join computePar181AndAbovedays h 
-- on a.loanid=h.loanid 

上面的代碼的總和加入計算的年齡

的代碼是工作的罰款,並計算罰款

enter image description here

,但是當我添加的選擇,我得到一個錯誤的更多加入

left join computePar181AndAbovedays h 
on a.loanid=h.loanid 

問題是我一開始遇到的錯誤:

enter image description here

An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them.

我還需要更多的表才能加入我的查詢。

您能否提供方法來簡化這個查詢的高度讚賞

+1

旁註:你絕對應該避免在任何查詢區域設置特定的日期格式。例如'DATEADD(day,-7,'12/19/2016')'在你的查詢中。始終使用ISO格式,如[日期](https://msdn.microsoft。COM/EN-US /庫/ bb630352.aspx)。格式:'YYYY-MM-DD'或'YYYYMMDD'。 –

回答

1

顯然你是運行到極限那裏什麼改變這個限制是不可能跟你有查詢。遵循錯誤消息中給出的建議:簡化。

怎麼樣?那麼你有很多CTE的定義。編寫查詢的另一種方法是在實際查詢之前實現臨時表中的CTE,然後改爲使用臨時表。


例如,這CTE:

membersWithLoans as -- gets members with loan 
(
select a.memberid, a.loanid,a.loanamt,a.intamt 
from loanmst a 
where loandt<='12/19/2016' 
and status = 'O' 
) 

可以被物化到一個臨時表:

select a.memberid, a.loanid,a.loanamt,a.intamt 
into #membersWithLoans 
from loanmst a 
where loandt<='12/19/2016' 
and status = 'O' 

這將創建一個臨時表#membersWithLoans可在進一步的臨時使用表格創作或在您的最終查詢中。

爲了進一步說明,假設您已將所有CTE物化爲臨時表,您將不再有WITH子句。這樣,你會最終使用臨時表在最終SELECT查詢:

-- create all temporary tables (one of them being #filterNegativeArrears) 

select 
    a.*, 
    -- the rest of your selections 
from 
    #filterNegativeArrears a 
    -- the rest of the joined temporary tables 
-- the rest of your query (WHERE, ORDER BY etc) 
+0

@曼茲嗨曼茲。不,您在'WITH'子句中定義的是CTE的** C ** ommon ** T ** able ** E ** xpressions。將它們與您臨時定義用於一個查詢的視圖進行比較。 CTE沒有實現;當你的查詢運行時,CTE的查詢在你的查詢中展開,類似於你如何用派生表寫你的查詢。 –

+0

這是我的一個新代碼。我會嘗試閱讀臨時表。我認爲SQL「與子句」是臨時表。我很快問題,但我可以加入我目前的子查詢與新的臨時表嗎?示例我將把輸出放到temp1,然後我將temp1加入到子查詢中? – Mandz

+0

@曼茲是的,你可以。我會很快更新我的答案來說明。 –