I have a scenario to display data from 2 tables combined. First table Named 'DayTable' consists of daily plan and actual. Second table named 'MonthTable' consists of Monthly plan and actual. I need to display last 6months data and the current month daily data.
My output is like
For this i wrote the query in 2 methods. One is using temp tables and another with out temp table. Please suggest the best apporach for this. Details are below
Approach 1: Using Temp Table
Declare @startdate date = CONVERT(DATE, DATEADD(dd, -DAY(DATEADD(MONTH, 0, GETDATE())) + 1, DATEADD(MONTH, 0, GETDATE()))) Declare @endDate date = DATEADD(DAY, -DAY(DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE())) CREATE TABLE #TEMP ( PlanDate NVARCHAR(100), [PastTrend - Plan] INT, [PastTrend - Actual] INT, [Current - Plan] INT, [Current - Actual] INT, ) ;With cte as ( Select @startdate sDate Union All Select DATEADD(day,1,sDate) From cte where DATEADD(day,1,sDate) <= @endDate ) INSERT INTO #TEMP SELECT REPLACE(CONVERT(CHAR(6), A.sDate, 106),' ',' - ') PlanDate ,NULL AS [PastTrend - Plan] ,NULL AS [PastTrend - Actual] ,SUM(B.PlanQuantity) AS [Current - Plan] ,SUM(B.Actual) AS [Current - Actual] FROM cte A LEFT OUTER JOIN DayTable B ON A.sDate = CONVERT(DATE,B.PlanDate) GROUP BY A.sDate SELECT * FROM ( SELECT CONVERT(CHAR(3), datename(month,PlanMonth)) + ' ' + RIGHT(CONVERT(VARCHAR(4), YEAR(PlanMonth)), 2) AS PlanDate ,SUM(PlanQuantity) AS [PastTrend - Plan] ,SUM(Actual) AS [PastTrend - Actual] ,NULL AS [Current - Plan] ,NULL AS [Current - Actual] FROM MonthTable WHERE CONVERT(DATE, PlanMonth) >= CONVERT(DATE, DATEADD(dd, -DAY(DATEADD(MONTH, 0, GETDATE())) + 1, DATEADD(MONTH, -6, GETDATE()))) group by PlanMonth UNION ALL
SELECT PlanDate ,[PastTrend - Plan] ,[PastTrend - Actual] ,[Current - Plan] ,[Current - Actual] FROM #TEMP ) T1 DROP TABLE #TEMP
When i use Actual execution plan, Query Cost (relative to the batch) : 90%
Approach 2: With out temp Table
Declare @startdate date = CONVERT(DATE, DATEADD(dd, -DAY(DATEADD(MONTH, 0, GETDATE())) + 1, DATEADD(MONTH, 0, GETDATE()))) Declare @endDate date = DATEADD(DAY, -DAY(DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE())) ;With cte as ( Select @startdate sDate Union All Select DATEADD(day,1,sDate) From cte where DATEADD(day,1,sDate) <= @endDate ) SELECT T1.PlanDate ,T1.[PastTrend - Plan] ,T1.[PastTrend - Actual] ,T1.[Current - Plan] ,T1.[Current - Actual] FROM ( SELECT A.sDate AS OriginalDate ,REPLACE(CONVERT(CHAR(6), A.sDate, 106),' ',' - ') PlanDate ,NULL AS [PastTrend - Plan] ,NULL AS [PastTrend - Actual] ,SUM(B.PlanQuantity) AS [Current - Plan] ,SUM(B.Actual) AS [Current - Actual] FROM cte A LEFT OUTER JOIN DayTable B ON A.sDate = CONVERT(DATE,B.PlanDate) GROUP BY A.sDate UNION ALL SELECT PlanMonth AS OriginalDate ,CONVERT(CHAR(3), datename(month,PlanMonth)) + ' ' + RIGHT(CONVERT(VARCHAR(4), YEAR(PlanMonth)), 2) AS PlanDate ,SUM(PlanQuantity) AS [PastTrend - Plan] ,SUM(Actual) AS [PastTrend - Actual] ,NULL AS [Current - Plan] ,NULL AS [Current - Actual] FROM MonthTable WHERE CONVERT(DATE, PlanMonth) >= CONVERT(DATE, DATEADD(dd, -DAY(DATEADD(MONTH, 0, GETDATE())) + 1, DATEADD(MONTH, -6, GETDATE()))) group by PlanMonth ) T1 ORDER BY T1.OriginalDateHere Query Cost (relative to the batch) : 100%
So can you suggest the best one. Actually i wrote the second method to avoid temp tables. If any failed in the query after create temp table, it will deleted. To avoid like problems and for simplicty i prefer the second. one. But now i am confused which is the best one in the performance wise also.