【发布时间】:2021-03-10 00:55:42
【问题描述】:
我被以下 WITH 部分所困扰。当 startdate 和 enddate 条件不满足时没有结果时,查询需要很长时间并最终返回超时错误。当有符合周期的记录时,结果是正常的,没有错误。
ILY as (
select
A.[EANcode] as 'EAN',
FL.[ContractId] as 'FacContractId',
SUM(case when FL.[Tp] = 'Dal' AND FL.[LijnType] = 'Statement' then FL.[Hoeveelheid] else 0 end) as 'Dal_Statement',
SUM(case when FL.[Tp] = 'Dal' AND FL.[Lt] = 'Account' then FL.[Hoeveelheid] else 0 end) as 'Dal_Account',
SUM(case when FL.[Tp] = 'Dal' AND FL.[Lt] = 'InvoicedAccount' then FL.[Hoeveelheid] else 0 end) as 'Dal_InvoicedAccount',
SUM(case when FL.[Tp] = 'Piek' AND FL.[Lt] = 'Statement' then FL.[Hoeveelheid] else 0 end) as 'Piek_Statement',
SUM(case when FL.[Tp] = 'Piek' AND FL.[Lt] = 'Account' then FL.[Hoeveelheid] else 0 end) as 'Piek_Account',
SUM(case when FL.[Tp] = 'Piek' AND FL.[Lt] = 'InvoicedAccount' then FL.[Hoeveelheid] else 0 end) as 'Piek_InvoicedAccount',
SUM(case when FL.[Tp] = 'Enkel' AND FL.[Lt] = 'Statement' then FL.[Hoeveelheid] else 0 end) as 'Enkel_Statement',
SUM(case when FL.[Tp] = 'Enkel' AND FL.[Lt] = 'Account' then FL.[Hoeveelheid] else 0 end) as 'Enkel_Account',
SUM(case when FL.[Tp] = 'Enkel' AND FL.[Lt] = 'InvoicedAccount' then FL.[Hoeveelheid] else 0 end) as 'Enkel_InvoicedAccount',
SUM(case when FL.[Tp] = 'TL Dal' AND FL.[Lt] = 'Statement' then FL.[Hoeveelheid] else 0 end) as 'TLDal_Statement',
SUM(case when FL.[Tp] = 'TL Dal' AND FL.[Lt] = 'Account' then FL.[Hoeveelheid] else 0 end) as 'TLDal_Account',
SUM(case when FL.[Tp] = 'TL Dal' AND FL.[Lt] = 'InvoicedAccount' then FL.[Hoeveelheid] else 0 end) as 'TLDal_InvoicedAccount',
SUM(case when FL.[Tp] = 'TL Piek' AND FL.[Lt] = 'Statement' then FL.[Hoeveelheid] else 0 end) as 'TLPiek_Statement',
SUM(case when FL.[Tp] = 'TL Piek' AND FL.[Lt] = 'Account' then FL.[Hoeveelheid] else 0 end) as 'TLPiek_Account',
SUM(case when FL.[Tp] = 'TL Piek' AND FL.[Lt] = 'InvoicedAccount' then FL.[Hoeveelheid] else 0 end) as 'TLPiek_InvoicedAccount'
from {FactuurLijn} FL
inner join {Factuur} FAC on FAC.[Id] = FL.[FactuurId]
inner join {FactuurContract} FC on FC.[FactuurId] = FAC.[Id]
inner join {Aansluiting} A on A.[Id] = FC.[AansluitingId]
inner join {PriceComponent} PC on PC.[Id] = FL.[PrijscomponentId]
where A.[EANcode] in (select getContracts.[EANCode] from getContracts)
and FC.[ContractId] in (select getContracts.[ContractId] from getContracts where getContracts.[ContractNummer] <> '')
and (FL.[StartDate] >= @StartDate and FL.[EndDate] <=@EndDate)
and PC.[PriceType] = 'VP'
and PC.[PriceComp] = 'Levering'
and FAC.[Show] = 1
and FL.[ContractId] <> 0
group by A.[EANcode], FL.[ContractId], FL.[Tp], FL.[Lt]
)
select .....
我已将联接更改为左联接,并将 sum 子句更改为 ISNULL(SUM(...),0) 但这没有任何效果。 (我更改了一些列名以提高这篇文章的可读性)。
【问题讨论】: