【问题标题】:SQL Server error simplify code due to multiple joins由于多个联接,SQL Server 错误简化了代码
【发布时间】:2017-03-22 22:00:59
【问题描述】:

我需要一种替代方法来简化此代码。

此代码根据贷款的还款时间表计算客户余额的年龄。过滤器是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

上面的代码加入了计算的年龄

代码运行良好,计算良好

但是当我在选择中添加更多加入时,我得到一个错误

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

问题是我开始遇到错误:

已达到表达服务限制。请寻找 查询中可能存在复杂的表达式,并尝试简化 他们。

我仍然需要更多表来加入我的查询。

您能提出一些方法来简化这个查询吗?非常感谢

【问题讨论】:

  • 旁注:您应该绝对避免在任何查询中使用特定于区域设置的日期格式。例如 DATEADD(day,-7,'12/19/2016') 在您的查询中。始终使用DATE 中所示的 ISO 格式。格式:YYYY-MM-DDYYYYMMDD

标签: sql sql-server-2008 tsql sql-server-2005


【解决方案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)

【讨论】:

  • @Mandz 嗨 Mandz。不,您在 WITH 子句中定义的是 CTE 的又名 Common Table Expressions。将它们与您临时定义以在一个查询中使用的视图进行比较。 CTE 没有实现;当您的查询运行时,CTE 会在您的查询中展开,类似于您使用派生表编写查询的方式。
  • 这对我来说是一个新代码。我会尝试阅读临时表。我认为sql“with子句”是临时表。我快速提问,但我可以将我当前的子查询与新的临时表一起加入吗?示例 我将把我的输出放到一个 temp1 然后我将 temp1 加入到之前的子查询中?
  • @Mandz 是的,你可以。我会很快更新我的答案来说明。
  • 这个查询正在从我的程序馈送到数据库。我的问题是,在从连接表中获得最终结果后,是否需要一些代码来处理临时表?示例我调用了我的代码 dbsqlConnect.Close();。临时表会保留还是会被删除?坦率地说,我正在使用这个查询来生成计算报告。这样我就知道如何采用我的程序。感谢时间
  • @Mandz 如果您有连接池,例如来自 JBoss 应用程序服务器,您肯定必须在 SELECT 语句之后立即删除临时表。因此,您的查询将是一个整体:(1) 创建临时表,(2) 引用临时表的 select 语句,最后 (3) 删除在步骤 1 中创建的所有临时表(例如 DROP TABLE #filterNegativeArrears; DROP TABLE ...)。这一切都在一个 SQL 脚本中。
猜你喜欢
  • 2021-11-19
  • 2014-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 2014-05-12
相关资源
最近更新 更多