SQL Server:如何使用DATEADD减去1个月

时间:2015-01-30 16:54:17

标签: sql-server

以下查询返回计算出的TotalScore并按日期返回。一切都是正确的,除非所有日期都落后于它们应该是1个月。因此,Jan显示为12月,但仍然保持Jan的正确分数,其余几个月也是如此。我希望保持尽可能多的查询完整,但只是停止返回-1个月。

    DECLARE @ProviderID INT, @Now DATETIME, @Months INT

    SELECT @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
            WHERE a.ProviderID = 874 AND GroupID = 2
            )  
        SELECT ProviderID, RowNum, StartDate, (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore
        FROM main_cte
        WHERE ordinal_position = 1
        ORDER BY RowNum

结果

ProviderID  RowNum      StartDate               TotalScore
----------- ----------- ----------------------- -----------
874         1           2014-12-30 10:45:22.000 95
874         2           2014-11-30 10:45:22.000 80
874         3           2014-10-30 10:45:22.000 55
874         4           2014-09-30 10:45:22.000 100
874         5           2014-08-30 10:45:22.000 50

期望的结果

ProviderID  RowNum      StartDate               TotalScore
----------- ----------- ----------------------- -----------
874         1           2015-01-30 10:45:22.000 95
874         2           2014-12-30 10:45:22.000 80
874         3           2014-11-30 10:45:22.000 55
874         4           2014-10-30 10:45:22.000 100
874         5           2014-09-30 10:45:22.000 50

表格ProviderValueCard,其中包含值。可以看到那里的日期。

    ProviderID  AdditionalReports UniqueReportRequests Time_Stamp              AdditionalReportsNum UniqueReportsNum SessionsProgress AdditionalReportsNumQtr UniqueReportsNumQtr SurveyCompleted
    ----------- ----------------- -------------------- ----------------------- -------------------- ---------------- ---------------- ----------------------- ------------------- ---------------
    874         0                 1                    2015-01-30 08:13:44.660 0                    55               3                0                       10                  1
    874         0                 0                    2014-12-30 08:31:20.893 0                    0                3                0                       0                   1
    874         1                 0                    2014-11-30 09:17:34.280 55                   0                0                20                      0                   0
    874         1                 1                    2014-10-30 10:17:52.940 5                    5                3                5                       5                   1
    874         0                 0                    2014-10-30 10:17:17.437 0                    0                1                0                       0                   1
    874         0                 0                    2014-09-30 10:20:56.163 0                    0                0                0                       0                   0
    874         0                 0                    2014-08-30 10:21:58.283 0                    0                0                0                       0                   0

我需要保留当前月份和分数的一小部分内容,我需要查询:SQL Server: How to return value for each past 6 months

1 个答案:

答案 0 :(得分:1)

我认为您需要将EndDate添加到main_cte并在select中使用它而不是StartDate。

DECLARE @ProviderID INT, @Now DATETIME, @Months INT

SELECT @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.EndDate ORDER BY Time_Stamp DESC) AS ordinal_position,
            a.ProviderID, 
            d.RowNum, 
            d.EndDate,
            [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
            WHERE a.ProviderID = 874 AND GroupID = 2
            )  
        SELECT ProviderID, RowNum, EndDate, (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore
        FROM main_cte
        WHERE ordinal_position = 1
        ORDER BY RowNum