2014-09-04 150 views
0

我有一個查詢,返回是否3列= 1,然後如果第二個表中存在ProviderID得分。我需要能夠使用Time_Stamp列的每個月爲6個月返回一個分數,不包括當前月份。下面將返回上個月的分數。我怎樣才能包括剩下的5個月和ROW_NUMBER()呢?SQL Server:如何返回每個過去6個月的值

DECLARE @ProviderID int = '1717'; 

WITH cte as 
(
    SELECT TOP 1 
     a.ProviderID, Time_Stamp, 
     SUM(CASE WHEN [AdditionalReports] = '1' THEN 5 ELSE 0 END) as AdditionalReports, 
     SUM(CASE WHEN [UniqueReportRequests] = '1' THEN 15 ELSE 0 END) as UniqueReportsRequests, 
     SUM(CASE WHEN [SurveyCompleted] = '1' THEN 30 ELSE 0 END) as SurveyCompleted, 
     MAX(CASE WHEN b.ProviderID IS NULL THEN 0 ELSE 50 END) as SubscriptionExists 
    FROM 
     ProviderValueCard a 
    LEFT JOIN 
     SubscriptionsTV b ON a.ProviderID = b.ProviderID 
    WHERE 
     a.ProviderID = @ProviderID AND GroupID = 2 
     AND Time_Stamp BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(DAY, -(DAY(GETDATE())), GETDATE()) 
    GROUP BY 
     Time_Stamp, a.ProviderID, event 
    ORDER BY 
     event DESC, Time_Stamp DESC 
) 
SELECT 
    ProviderID, Time_Stamp, 
    (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore 
FROM 
    cte 

這裏是如何抓住前幾個月的第一/最後一天:

2個月前:

DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0) as FirstD2monthsago, 
     DATEADD(DAY, -DAY(GETDATE()), DATEADD(MONTH, -1, GETDATE())) AS last_day_2_months_ago 

3個月前等:

DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 3, 0) as FirstD3monthsago, 
     DATEADD(DAY, -DAY(GETDATE()), DATEADD(MONTH, -2, GETDATE())) AS last_day_3_months_ago 

所需的輸出

ProviderID Time_Stamp    TotalScore Row_Number 
----------- ----------------------- ----------- 
1717  2014-08-29 12:11:17.610 70    1 
1717  2014-07-29 12:11:17.610 95    2 
1717  2014-06-29 12:11:17.610 100    3 
1717  2014-05-29 12:11:17.610 70    4 
1717  2014-04-29 12:11:17.610 70    5 
1717            6 
+0

你爲什麼刪除[問題](http://stackoverflow.com/questions/25668997/sql-server-including-each-of-last-6-months?noredirect=1#comment40114729_25668997)和重新發布嗎? – Ram 2014-09-04 17:25:52

+0

你能幫我把它拼在一起嗎?我不確定我每次參加哪個活動或在哪裏加入ROW_NUMBER – user3749447 2014-09-04 17:29:05

回答

2
DECLARE @ProviderID INT, @Now DATETIME, @Months INT 
SELECT @ProviderID = 1717, @Now = GETDATE(), @Months = 5 

WITH 
date_range_cte AS (
    SELECT 1 AS RowNum, DATEADD(mm,-1,@Now) AS StartDate, DATEADD(mm,0,@Now) AS EndDate 
    UNION ALL 
    SELECT d.RowNum + 1 AS RowNum, DATEADD(mm,(-d.RowNum - 1),@Now) AS StartDate, DATEADD(mm,-d.RowNum,@Now) AS EndDate 
    FROM date_range_cte d 
    WHERE d.RowNum + 1 <= @Months 
    ), 
    main_cte AS (
      SELECT 
      ROW_NUMBER() OVER (PARTITION BY a.ProviderID, d.RowNum, d.StartDate ORDER BY Time_Stamp DESC) AS ordinal_position, 
      a.ProviderID, 
      d.RowNum, 
      d.StartDate, 
      [AdditionalReports] * 5 AS AdditionalReports, 
      [UniqueReportRequests] * 15 AS UniqueReportsRequests, 
      [SurveyCompleted] * 30 as SurveyCompleted, 
      CASE WHEN b.ProviderID IS NULL THEN 0 ELSE 50 END as SubscriptionExists 
      FROM ProviderValueCard a 
      INNER JOIN date_range_cte d ON d.StartDate < Time_Stamp AND Time_Stamp <= d.EndDate 
      LEFT OUTER JOIN SubscriptionsTV b ON a.ProviderID = b.ProviderID 
      WHERE a.ProviderID = @ProviderID AND GroupID = 2 
      ) 
     SELECT ProviderID, RowNum, StartDate, (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore 
     FROM main_cte 
     WHERE ordinal_position = 1 
     ORDER BY RowNum 
+0

Jim,表格每月可能有多個條目,爲了防止重複的添加,我在'Time_Stamp'命令時只選擇了'top 1' 。我正在玩弄它,但我怎麼能阻止呢? – user3749447 2014-09-04 18:14:38

+0

我也不確定分數是如何加入的。六月應該100(最高分),但是是500,並且這個月只有一個條目 – user3749447 2014-09-04 18:37:56

+0

我更新瞭解決方案。如果三個字段是位,則可能必須將它們轉換爲整數。如果它們可以爲空,則可能需要應用isNull。 – 2014-09-04 18:55:19

0

這裏有幾種方法(psuedocode):

1 - 爲每個想要獲得的月份製作一個cte,就像您現有的cte一樣。你需要在每一個改變的唯一事情是這一行:

AND Time_Stamp BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(DAY, -(DAY(GETDATE())), GETDATE()) 

2個月前,你將它改成這樣:

AND Time_Stamp BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0) AND DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) 

等爲其他月份回來。

2 - 參考上述cte中的同一行,將其更改爲-6以獲取過去6個月的數據。然後在您的選擇列表中包含一個MONTH(TimeStamp)列,並按它分組,以在過去6個月內每月獲得一行。

+0

您可以在頭兩個月爲我設計一個查詢嗎?我不知道從以前的cte選擇/傳遞什麼 – user3749447 2014-09-04 18:21:38

+0

這基本上是我所做的。第一行來自上個月的CTE,第二行來自CTE。 – 2014-09-04 20:01:40

+0

我把這個包括在我的問題中......我已經抓住了最後一個月 – user3749447 2014-09-04 20:10:04