sql 这段查询语句如何优化?

qq_21854151 2017-02-16 09:59:50
select b.ID,a.MainID,a.BorrowerName,a.TransferAmoun,a.APPLoanTerm,a.StartDate,a.EndDate,a.APPTime,a.InterestRate,a.JujianRate,a.Repayment,a.Assessment,a.Disposable,c.Charge,c.ProjectStatus,a.Explain,a.InputName,a.InputTime,b.Period,b.RepaymentDate,DATEDIFF(day,getdate(),b.RepaymentDate) as SurplusDays,--剩余天数
b.Principal,b.Interest,b.Mediacy,b.Subtotal,b.TotalAmount,b.Remarks,c.BusinessType,c.Guarantee,
(case when b.TotalAmount-(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and (a1.RepaymentType='居间' or a1.RepaymentType='利息' or RepaymentType='本金'))>0 and DATEDIFF(day,getdate(),b.RepaymentDate)<0 then b.TotalAmount-(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and (a1.RepaymentType='居间' or a1.RepaymentType='利息' or RepaymentType='本金')) else 0 end ) as ResidualAmount,--未还金额
(case when b.TotalAmount-(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and (a1.RepaymentType='居间' or a1.RepaymentType='利息' or RepaymentType='本金'))>0 and DATEDIFF(day,getdate(),b.RepaymentDate)<0 then '逾期' else '正常' end ) as ResidualStatus,--是否逾期
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and a1.RepaymentType='本金') as Principal_H, --已还本金
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and a1.RepaymentType='利息') as Interest_H, --已还利息
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and a1.RepaymentType='居间') as Mediacy_H, --已还居间
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and (a1.RepaymentType='居间' or a1.RepaymentType='利息' )) as Subtotal_H, --费用合计
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and (a1.RepaymentType='居间' or a1.RepaymentType='利息' or RepaymentType='本金')) as TotalAmount_H,--应还总费用
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and a1.RepaymentType='逾期违约金') as Penalty1_H,
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and a1.RepaymentType='提前还款违约金') as Penalty2_H,
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and a1.RepaymentType='调查评估费') as Assessment_H,
(select isnull(sum(a1.TotalAmount),0) from RepaymentRegister a1 where a1.ProjectNo=b.MainID and a1.Period=b.Period and a1.RepaymentType='前期费用') as Disposable_H
from ht_Repayments1 a inner join ht_Repayments2 b on a.MainID=b.MainID inner join HT_CreditInput c on a.MainID=c.MainID

请各位高手,帮忙看看是否可以优化一下,以提高速度,目前数量大查询速度特别慢。不胜感激。
...全文
82 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

249

社区成员

发帖
与我相关
我的任务
社区描述
其他产品/厂家
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧