使用多个CTE

时间:2014-10-09 11:54:49

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

我正在为每个CTE获得重复的结果,因为我正在使用Group BY for Months Data。这是查询的一部分,问题在哪里。在CTE上创建的列的基础上进行计算。 请帮忙...

DECLARE @StartDate datetime
SET @StartDate='12/01/2012'
WHILE @StartDate<'12/02/2012' --Set GETDATE()
BEGIN
WITH CTE3 (Parameter,Month3,Count3,Count2,Count1,DateRange,OrgID)    
 AS    
  (    
  SELECT CAST(Col1 AS VARCHAR(50)) Parameter
  ,SUM(CASE WHEN DateRange=CAST(LEFT(DATENAME(MONTH,DATEADD(MONTH,DATEDIFF(M,0,@StartDate),0)),3) + '-' + DATENAME(YYYY,@StartDate) AS VARCHAR(50))
  THEN Col2 else 0 END) Month3
  ,(SELECT COUNT(Col1) FROM [Table_Name] 
  WHERE Col1='INS Charges'   
  AND DateRange = CAST(LEFT(DATENAME(MONTH,DATEADD(MONTH,DATEDIFF(M,0,@StartDate),0)),3) + '-' + DATENAME(YYYY,@StartDate) AS VARCHAR(50))) Count3
  ,(SELECT COUNT(Col1) FROM [Table_Name]
  WHERE DateRange = CAST(LEFT(DATENAME(MONTH,DATEADD(MONTH,DATEDIFF(M,0,@StartDate)-1,0)),3) + '-' + DATENAME(YYYY,@StartDate) AS VARCHAR(50))) Count2  
  ,(SELECT COUNT(Col1) FROM [Table_Name]   
  WHERE DateRange = CAST(LEFT(DATENAME(MONTH,DATEADD(MONTH,DATEDIFF(M,0,@StartDate)-2,0)),3) + '-' + DATENAME(YYYY,@StartDate) AS VARCHAR(50))) Count1  
  ,DateRange
  ,OrgID
  FROM [Table_Name]
  WHERE DateRange=CAST(LEFT(DATENAME(MONTH,DATEADD(MONTH,DATEDIFF(M,0,@StartDate),0)),3) + '-' + DATENAME(YYYY,@StartDate) AS VARCHAR(50))
  GROUP BY Col1,OrgID,DateRange
  )
  ,CTE2 (Parameter,Month2,DateRange,OrgID)
  AS
  (
    SELECT CAST(Col1 AS VARCHAR(50)) Parameter
    ,SUM(Col2) Month2
    ,DateRange
    ,OrgID
    from [Table_Name]
    WHERE DateRange= CAST(LEFT(DATENAME(MONTH,DATEADD(MONTH,DATEDIFF(M,0,@StartDate)-1,0)),3) + '-' + DATENAME(YYYY,@StartDate) AS VARCHAR(50))
    GROUP BY Col1,OrgID,DateRange
  )
  ,CTE1 (Parameter,Month1,DateRange,OrgID)
  AS
  (
    SELECT CAST(Col1 AS VARCHAR(50)) Parameter
    ,SUM(Col2) Month1
    ,DateRange
    ,OrgID
    from [Table_Name]
    WHERE DateRange= CAST(LEFT(DATENAME(MONTH,DATEADD(MONTH,DATEDIFF(M,0,@StartDate)-2,0)),3) + '-' + DATENAME(YYYY,@StartDate) AS VARCHAR(50))
    GROUP BY Col1,DateRange,OrgID
  )
  SELECT CTE3.Parameter    
  ,CTE1.Month1
  ,CTE2.Month2    
  ,CTE3.Month3
  ,CTE1.Month1+CTE2.Month2+CTE3.Month3 YTD
  ,(CTE1.Month1+CTE2.Month2+CTE3.Month3)/3 [3 Month Avg]
  ,Cte3.OrgID
  ,Cte3.DateRange
  FROM CTE3
  JOIN CTE1 ON CTE1.Parameter=CTE3.Parameter
  JOIN CTE2 ON CTE2.Parameter=CTE3.Parameter
  SET @StartDate=(SELECT DATEADD(M,1,@StartDate))
  END

样本数据

TABLE_NAME


Col1  | Col2  | OrgID | DateRange|
ABC1  |100    |1      |Jan-2014  |
ABC2  |200    |2      |Feb-2014  |
ABC1  |300    |3      |Jan-2014  |
ABC2  |400    |1      |Feb-2014  |
ABC1  |100    |2      |Mar-2014  |
ABC2  |200    |3      |Mar-2014  |
--------------------------------------

这是过去3个月的报告,其中包含复杂的计算方法。如果您注意到table_Name只包含DateRange,我必须在其中创建列中的Last 3 Months数据,并根据上述查询提取的数据(即Month1,Month2等),必须执行数学运算。这就是我选择CTE的原因。

我能够生成结果,但唯一的问题是每列获得重复的行,因为每个CTE都有Group By。

如果有任何替代方案,请告诉我!!!!

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

您应该在其GROUP BY子句的每一列上加入CTE-s,例如:

FROM CTE3
JOIN CTE1 ON CTE1.Parameter=CTE3.Parameter AND CTE1.OrgID=CTE3.OrgID AND CTE1.DateRange=CTE3.DateRange
JOIN CTE2 ON CTE2.Parameter=CTE3.Parameter AND CTE2.OrgID=CTE3.OrgID AND CTE2.DateRange=CTE3.DateRange

(假设这些列不允许NULL)