确定连续的时间段

时间:2014-12-02 19:51:21

标签: sql sql-server-2008-r2

今天我的问题与标记满足给定标准的连续时间段有关。我感兴趣的原始数据看起来像这样。

Salesman ID        Pay Period ID         Total Commissionable Sales (US dollars)
 1                    101                       525
 1                    102                       473
 1                    103                       672
 1                    104                       766 
 2                    101                       630
 2                    101                       625
 .....

我想标记销售员达到500美元或更多销售额的连续时间段。我理想的结果应该是这样的。

[Salesman ID]    [Start time]     [End time]    [# Periods]    [Average Sales]
      1             101              101          1               525
      1             103              107          5               621   
      2             101              103          3               635
      3             104              106          3               538     

我知道如何处理其他事情,但我无法找出一种非超级昂贵的方法来识别开始和结束日期。救命啊!

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。最里面的select语句基本上将一个新列添加到原始表中,并带有一个标志,用于确定新组的开始时间。在此声明之外,我们在运行总计中使用此标志,然后枚举组 - 我们将此列称为[组ID]。剩下的就是过滤掉[Sales]< 500,并按[推销员ID]和[组ID]分组。

SELECT [Salesman ID], MIN([Pay Period ID]) AS [Start time],
    MAX([Pay Period ID]) AS [End time], COUNT(*) AS [# of periods],
    AVG([Sales]) AS [Average Sales]
FROM (
    SELECT [Salesman ID], [Pay Period ID], [Sales],
        SUM(NewGroup) OVER (PARTITION BY [Salesman ID] ORDER BY [Pay Period ID]
                            ROWS UNBOUNDED PRECEDING) AS [Group ID]
    FROM (
        SELECT T1.*,
            CASE WHEN T1.[Sales] >= 500 AND (Prev.[Sales] < 500 OR Prev.[Sales] IS NULL)
                 THEN 1 ELSE 0 END AS [NewGroup]
        FROM MyTable T1
        LEFT JOIN MyTable Prev ON Prev.[Salesman ID] = T1.[Salesman ID] 
                               AND Prev.[Pay Period ID] = T1.[Pay Period ID] - 1
    ) AS InnerQ
) AS MiddleQ
WHERE [Sales] >= 500
GROUP BY [Salesman ID], [Group ID]