子查询中的CTE

时间:2014-04-23 04:24:22

标签: sql sql-server subquery common-table-expression

我有一个方案来显示来自2个表的数据。名为'DayTable'的第一个表包含每日计划和实际计划。名为“MonthTable”的第二个表包含每月计划和实际计划。我需要显示最近6个月的数据和当前月份的每日数据。所以我写了下面的查询i为预期的输出

 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 
--ORDER 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

我的输出就像

Output Image

现在我想避免使用临时表概念,因为如果在创建临时表后出现任何故障,它将不会丢失。所以重写下面的查询

 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 
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 

ORDER BY OriginalDate

但这里我有问题。在输出中我不需要OriginalDate。如何避免这种情况。为此,我可以将联合输出包装到选择查询,但我怎么能在cte中得到错误。请指导我。还建议哪种方法最好

1 个答案:

答案 0 :(得分:0)

我完成了查询。实际上,我只是将查询包装为外部而不是cte。 Cte必须是查询中的顶部。最后的查询是

    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.OriginalDate

但我需要知道peroformance。当我用实际执行计划执行此查询时查询成本(相对于批处理):100% 当我使用temp执行第一个方法时,查询成本为90%。谁能指导这个