Gaps and Islands ? Or Not?

时间:2015-10-29 15:48:51

标签: sql-server tsql sql-server-2012 gaps-and-islands

I have the following table:

;with data as (
select '508325'  as [customer], 'G61' as [demo_given],
        cast('2015-1-1' as date) as [date_of_demo]
union all select '508325', 'G61', cast('2015-3-1' as date) 
union all select '508325', 'G61',cast('2015-3-15' as date)
union all select '508325', 'G61',cast('2015-3-16' as date)
union all select '508325', 'G61',cast('2015-3-17' as date)
union all select '508325', 'G61',cast('2015-6-1' as date)
union all select '508325', 'G61',cast('2015-8-1' as date)
union all select '508325', 'G61',cast('2015-9-1' as date)
union all select '508325', 'G61',cast('2015-9-1' as date)
union all select '508325', 'G61',cast('2015-12-1' as date)
)

Per customer there may be only 3 demos given in a 4 month period. The first period starts counting at the first demo given and ends 4 months later.

If the number of demos in that period exceeds 3 I need the dates of the demos 4 and later in that 4 month period. (that would be 2015-3-16 and 2015-3-17 in this example)

The next period starts at the date of the first demo given after the first four months. So I need to count the number of demos in the period 2015-6-1 up until 2015-9-30 and return the dates of the eventual 'surplus' demos given in that period.

How would I go about doing this?

1 个答案:

答案 0 :(得分:2)

我已经使用多步CTE来提高可读性,但如果您愿意,可以将它组合使用:

  • tally - 简单的数字表,你可以使用你想要的任何方法(递归cte,虚拟表,表函数,......)
  • min_date_per_customer - 获取每位客户的第一个演示日期
  • date_ranges - 生成向min_date添加4个月的范围
  • final - 将data加入date_ranges,生成行号
  • main query - 过滤掉特定时期内第4,第5,第6 ......的演示

代码:

WITH tally(N) AS (
  SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
  FROM sys.all_columns a CROSS JOIN sys.all_columns b
), min_date_per_customer AS (
  SELECT customer, MIN(date_of_demo) AS min_date
  FROM #data
  GROUP BY customer
), date_ranges AS (
  SELECT t.N, mdpc.customer
      ,[date_start] = DATEADD(m, 4 * (t.N - 1), min_date)
      ,[date_end]   = DATEADD(m, 4 *t.N, min_date)
  FROM min_date_per_customer mdpc
  CROSS JOIN tally t
  WHERE t.N < 100 -- you can generate as many period as you wish
), final AS (
  SELECT d.customer
       ,d.demo_given
       ,d.date_of_demo
       ,dr.N
       ,rn = ROW_NUMBER() OVER (PARTITION BY dr.customer, dr.N ORDER BY date_of_demo)
  FROM #data d
  JOIN date_ranges dr
    ON d.[date_of_demo] >= dr.date_start
   AND d.[date_of_demo] <= dr.date_end
   AND d.customer = dr.customer
)
SELECT *
FROM final
WHERE rn > 3
ORDER BY customer, date_of_demo;

LiveDemo

输出:

╔══════════╦════════════╦═════════════════════╦═══╦═════╗
║ customer ║ demo_given ║    date_of_demo     ║ N ║ rn  ║
╠══════════╬════════════╬═════════════════════╬═══╬═════╣
║   508325 ║ G61        ║ 2015-03-16 00:00:00 ║ 1 ║   4 ║
║   508325 ║ G61        ║ 2015-03-17 00:00:00 ║ 1 ║   5 ║
║   508325 ║ G61        ║ 2015-09-01 00:00:00 ║ 2 ║   4 ║
╚══════════╩════════════╩═════════════════════╩═══╩═════╝