我有一个100K行表,表示特定时间段内的销售额。通常这段时间至少是几个小时,但偶尔我们会得到一段只有几分钟的时间。这些微小的时期扰乱了下游报告,因此我想将它们与上一期合并。任何30分钟或更短时间的期间都应与上一期间合并,销售数据按期间汇总。在长期之间可能存在零个,一个或多个后续短期。数据中没有时间间隔 - 一个句点的开头始终与前一个句点的结尾相同。
执行此合并的基于集合的方式(没有游标!)是什么?
现有数据(简化)如下所示:
UnitsSold Start End
---------------------------------------------------
10 06-12-2013 08:03 06-12-2013 12:07
12 06-12-2013 12:07 06-12-2013 16:05
1 06-12-2013 16:05 06-12-2013 16:09
1 06-12-2013 16:09 06-12-2013 16:13
7 06-12-2013 16:13 06-12-2013 20:10
所需的输出如下所示:
UnitsSold Start End
---------------------------------------------------
10 06-12-2013 08:03 06-12-2013 12:07
14 06-12-2013 12:07 06-12-2013 16:13
7 06-12-2013 16:13 06-12-2013 20:10
不幸的是我们仍然使用SQL Server 2008 R2,因此我们无法利用SQL Server 2012中的酷新窗口功能,这可能会使这个问题更容易有效地解决。
在Merge adjacent rows in SQL?中对类似问题进行了很好的讨论。我特别喜欢PIVOT / UNPIVOT解决方案,但我对如何使其适应我的问题感到困惑。
答案 0 :(得分:1)
我的想法是
像这样的东西
declare @t table (UnitsSold int, start datetime, finish datetime)
insert into @t values (10, '20130612 08:03', '20130612 12:07')
insert into @t values (12, '20130612 12:07', '20130612 16:05')
insert into @t values (1, '20130612 16:05', '20130612 16:09')
insert into @t values (1, '20130612 16:09', '20130612 16:13')
insert into @t values (7, '20130612 16:13', '20130612 20:10')
select
(select SUM(UnitsSold) from @t t3 where t3.start>=t1.start and t3.finish<=ISNULL(oa.start, t1.finish)) as UnitsSold,
t1.start,
ISNULL(oa.start, t1.finish) as finish
from @t t1
outer apply (
select top(1) start
from @t t2
where datediff(minute,t2.start, t2.finish)>30
and t2.start >= t1.finish
order by t2.start
) oa
where datediff(minute, t1.start, t1.finish)>30
答案 1 :(得分:0)
使用递归CTE:
DECLARE @t TABLE (UnitsSold INT, Start DATETIME, Finish DATETIME)
INSERT INTO @t VALUES
(10, '06-12-2013 08:03', '06-12-2013 12:07'),
(12, '06-12-2013 12:07', '06-12-2013 16:05'),
(1, '06-12-2013 16:05', '06-12-2013 16:09'),
(1, '06-12-2013 16:09', '06-12-2013 16:13'),
(7, '06-12-2013 16:13', '06-12-2013 20:10')
;WITH rec AS (
-- Returns periods > 30 minutes
SELECT u.UnitsSold, u.Start, u.Finish
FROM @t u WHERE DATEDIFF(MINUTE, u.Start, u.Finish) > 30
UNION ALL
-- Adds on adjoining periods <= 30 minutes
SELECT
u.UnitsSold + r.UnitsSold,
r.Start,
u.Finish
FROM rec r
INNER JOIN @t u ON r.Finish = u.Start
AND DATEDIFF(MINUTE, u.Start, u.Finish) <= 30)
-- Since the CTE also returns incomplete periods we need
-- to filter out the relevant periods, in this case the
-- last/max values for each start value.
SELECT
MAX(r.UnitsSold) AS UnitsSold,
r.Start AS Start,
MAX(r.Finish) AS Finish
FROM rec r
GROUP BY r.Start
答案 2 :(得分:0)
使用CTE和累积总和:
DECLARE @t TABLE (UnitsSold INT, Start DATETIME, Finish DATETIME)
INSERT INTO @t VALUES
(10, '06-12-2013 08:03', '06-12-2013 12:07'),
(12, '06-12-2013 12:07', '06-12-2013 16:05'),
(1, '06-12-2013 16:05', '06-12-2013 16:09'),
(1, '06-12-2013 16:09', '06-12-2013 16:13'),
(7, '06-12-2013 16:13', '06-12-2013 20:10')
;WITH groups AS (
SELECT UnitsSold, Start, Finish,
-- Cumulative sum, IIF returns 1 for each row that
-- should generate a new row in the final result.
SUM(IIF(DATEDIFF(MINUTE, Start, Finish) <= 30, 0, 1)) OVER (ORDER BY Start) csum
FROM @t)
SELECT
SUM(UnitsSold) UnitsSold,
MIN(Start) Start,
MAX(Finish) Finish
FROM groups
GROUP BY csum