I have this sql with Common Table Expression (CTE). This sql needs some tuning to overcome the execution time.How can we replace this CTE for better performance? Please don't comment if you are not interested. with wth_DateList As ( select GETDATE() as DateCol union all select DateCol + 1 from wth_DateList where DateDiff(d, getdate(),DateCol+1) < 91 and DateCol + 1 > GETDATE() union all select DateCol - 1 from wth_DateList where DateDiff(d, DateCol-1, getdate()) < 91 and DateCol - 1 < GETDATE() ), w_DateList as ( select CONVERT(varchar(15), DateCol, 101) DateCol from wth_DateList ) select DateCol [Date], ( select count(1) from SajhaUsers where CreatedDate between d.DateCol and dateadd(d,1, d.DateCol) ) [Sajha User Accounts Created], (select count(1) from SajhaApplication where SubmittedDate between d.DateCol and dateadd(d,1, d.DateCol)) [Sajha Thread Submitted], (select count(1) from Sajhapost where PostTypeId not in (2,4,3) and CreatedDate between d.DateCol and dateadd(d,1, d.DateCol)) [Sajha Ad Created], (select count(1) from Sajhapost where PostTypeId = 4 and CreatedDate between d.DateCol and dateadd(d,1, d.DateCol)) SajhaAnnounce, (select count(1) from Sajhapost where PostTypeId = 3 and CreatedDate between d.DateCol and dateadd(d,1, d.DateCol)) [SajhaEvents], (select count(1) from Sajhapost where PostTypeId = 2 and CreatedDate between d.DateCol and dateadd(d,1, d.DateCol)) [Sajha Messages], (select count(1) from SajhaThread where CreatedDate between d.DateCol and dateadd(d,1, d.DateCol)) [Sajha Threads Created], (select count(1) from SajhaComment where CreatedDate between d.DateCol and dateadd(d,1, d.DateCol)) [Sajha Comments], (select count(1) from SajhaSubmission where SajhaReportingDate between d.DateCol and dateadd(d,1, d.DateCol)) [Sajha Submissions] from w_DateList d where DateCol between GETDATE()-90 and getdate() order by 1 Last edited: 04-Sep-19 02:25 PM
Nepalibudho · Sep 4, 2019 1:52 PM · 91 views
This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.
Start a New Discussion