i have this query:
select sum(coalesce(PaidDeals.Value,0)) from
T_Deals Deals inner join T_PaidDeals PaidDeals on PaidDeals.DealId = Deals.DealId and PaidDeals.Valid = 1
where Deals.IdMain = @IdMain and
datediff(dd, 0, case when @UseDate = 1 then Deals.DealDate
when @UseDate = 2 then PaidDeals.PaidDate end) = datediff(dd, 0, getdate())
so that if the parameter @useDate =1 it will compare based on the deal date, otherwise will compare with the paid date.. but this query takes like 30sec with the case when, but like 15sec with the case when (no matter if i use datediff(dd, 0, Deal.DealDate) or datediff(dd, 0,UpdateTable.Date)..
so is using dynamic sql my only choice ? or is there anyway to improve it ?
also im using this query in a update like i create a temp table with X dates depending on the selected range with 2 col (Date, TotalValue) and then run an update temptable set TotalValue = queryabove does it matter also ?
does anyone have any ideia how i can improve it ?
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.