我正在尝试优化以下查询以提高性能,尤其是联接。我将不胜感激任何建议和帮助。非常感谢。我正在考虑使用 CTE 而不是子查询
询问:
Select year(dtb.tbDATE) as YearR,
`CONVERT(VARCHAR(12),dtb.tbDATE,110) as DateR,
(case when dtb.l_grp_no= 7 then COMPANY_B else COMPANY_A end ) as Portfolio,
(case when dtb.past_days between 5 and 30 then '5-30'
when dtb.past_days between 31 and 60 then'31-60'
when dtb.past_days between 61 and 90 then '61-90'
when dtb.past_days >= 91 then '91+'
when dtb.past_days <5 then 'Current' else 'Dis_po' end) 'Qdel',
case when lsc.SStatusC is not null and dtb.tbDATE> ls.eff_date
then lsc.SStatusC else 'XX'end as 'LStatus',
count(dtb.refaccno) as lcount,
sum (dtb.P_bal) as Lbal
from sln.[dbo].[table_dtb] as dtb
--left outer join sln.dbo.acct_l la on dtb.accrefno = la.accrefno
left outer join (select stat_acct_l.* from sln.dbo.stat_acct_l
inner join
(select refaccno, max(row_id) as MaxRow_id
from sln.dbo.stat_acct_l
group by refaccno) as maxStatus
on stat_acct_l.refaccno = maxStatus.refaccno
and stat_acct_l.row_id = maxStatus.MaxRow_id) as ls
on ls.refaccno = dtb.refaccno
left outer join dw.dbo.AccSt_C lsc on lsc.Stat_C_ID= ls.status_code_no
where dtb.l_grp_no in (7,4,8,15)and dtb.tbDATE > '2010-06-31'
and dtb.P_bal+dtb.l_C_bal >0
group by year(dtb.tbDATE), CONVERT(VARCHAR(12),dtb.tbDATE,110),
case when dtb.past_days between 5 and 30 then '5-30'
when dtb.past_days between 31 and 60 then'31-60'
when dtb.past_days between 61 and 90 then '61-90'
when dtb.past_days >= 91 then '91+'
when dtb.past_days <5 then 'Current' else 'Dis_po' end
case when dtb.l_grp_no= 7 then COMPANY_B else COMPANY_A end,
case when lsc.SStatusC is not null
and dtb.tbDATE> ls.eff_date Yearthen
lsc.SStatusC else 'XX'
end
order by year(dtb.tbDATE), CONVERT(VARCHAR(12),dtb.tbDATE,110),
case when dtb.l_grp_no= 7 then COMPANY_Belse COMPANY_A end