SQL Server:使用当前查询创建加权分数

时间:2015-02-12 16:21:04

标签: sql-server

以下查询通过获取过去6个月(包括当月)的最新得分来返回计算出的TotalScoreTotalScore是通过添加各种"得分来计算的。从表ProviderValueCard的几列中获取特定的ProviderID。如果SessionsProgress是>,则目前会将30添加到分数中1.如果SessionsProgress为0 + 0,1然后+10,2 + 20,3 + 20,4 + 30,5 +30,则需要缩放。

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

SELECT @Now = GETDATE(), @Months = 6;

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,
            [SessionsProgress] * 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 = 1672 AND GroupID = 2
            )  
        SELECT ProviderID, RowNum, EndDate, (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore
        FROM main_cte
        WHERE ordinal_position = 1
        ORDER BY RowNum

当前输出

ProviderID  RowNum      EndDate                 TotalScore
----------- ----------- ----------------------- -----------
1672        1           2015-02-12 10:09:27.420 100
1672        2           2015-01-12 10:09:27.420 70
1672        3           2014-12-12 10:09:27.420 70
1672        4           2014-11-12 10:09:27.420 55
1672        5           2014-10-12 10:09:27.420 70
1672        6           2014-09-12 10:09:27.420 55

表ProviderValueCard的第一行得分的2月入口。 SessionsProgess = 1,所以上面的2月得分为满30,但是只需得到10而不是得分为80,而不是100。

ProviderID  AdditionalReports UniqueReportRequests Time_Stamp              AdditionalReportsNum UniqueReportsNum SessionsProgress AdditionalReportsNumQtr UniqueReportsNumQtr SurveyCompleted
----------- ----------------- -------------------- ----------------------- -------------------- ---------------- ---------------- ----------------------- ------------------- ---------------
1672        1                 1                    2015-02-06 11:38:37.680 130                  6                1                0                       1                   1

1 个答案:

答案 0 :(得分:0)

CASE [SessionsProgress] WHEN 1 THEN 10
                                WHEN 2 THEN 20
                                WHEN 3 THEN 20
                                WHEN 4 THEN 30
                                WHEN 5 THEN 30
                                ELSE 0
         END as SurveyCompleted,