将时间范围与多行连接起来

Concatenate Time Ranges with multiple rows

在 SQL 服务器上,我查看了 return 以下员工数据。如您所见,它显示了 3 个相邻的时隙(FromTill 列)。

-----------------------------------------------------------------------------------------
| PerNr    | Name       | From       | Till       | CostCentre | RowNumber | Percentage |
-----------------------------------------------------------------------------------------
| 12345678 | Marco Polo | 2015-12-07 | 2015-12-09 | Centre1    |         1 |        0.5 |
| 12345678 | Marco Polo | 2015-12-07 | 2015-12-09 | Centre2    |         2 |        0.2 |
| 12345678 | Marco Polo | 2015-12-07 | 2015-12-09 | Centre3    |         3 |        0.3 |
| 12345678 | Marco Polo | 2015-12-10 | 2015-12-13 | Centre1    |         1 |        0.5 |
| 12345678 | Marco Polo | 2015-12-10 | 2015-12-13 | Centre2    |         2 |        0.2 |
| 12345678 | Marco Polo | 2015-12-10 | 2015-12-13 | Centre3    |         3 |        0.3 |
| 12345678 | Marco Polo | 2015-12-14 | 2015-12-16 | Centre1    |         1 |        0.6 |
| 12345678 | Marco Polo | 2015-12-14 | 2015-12-16 | Centre2    |         2 |        0.1 |
| 12345678 | Marco Polo | 2015-12-14 | 2015-12-16 | Centre3    |         3 |        0.3 |
-----------------------------------------------------------------------------------------

现在我想合并数据(除了 PerNr、From 和 Till 之外的所有数据)不变的块。结果应如下所示(注意第一组和第二组 3 行现在组合在一起):

-----------------------------------------------------------------------------------------
| PerNr    | Name       | From       | Till       | CostCentre | RowNumber | Percentage |
-----------------------------------------------------------------------------------------
| 12345678 | Marco Polo | 2015-12-07 | 2015-12-13 | Centre1    |         1 |        0.5 |
| 12345678 | Marco Polo | 2015-12-07 | 2015-12-13 | Centre2    |         2 |        0.2 |
| 12345678 | Marco Polo | 2015-12-07 | 2015-12-13 | Centre3    |         3 |        0.3 |
| 12345678 | Marco Polo | 2015-12-14 | 2015-12-16 | Centre1    |         1 |        0.6 |
| 12345678 | Marco Polo | 2015-12-14 | 2015-12-16 | Centre2    |         2 |        0.1 |
| 12345678 | Marco Polo | 2015-12-14 | 2015-12-16 | Centre3    |         3 |        0.3 |
-----------------------------------------------------------------------------------------

到目前为止我尝试的是运行以下语句:

SELECT result1.pernr, result1.name, result1.[from], result1.till, result1.costcentre, result1.rownumber, result1.percentage
INTO #concatenated
FROM #result result1
INNER JOIN #result result2 ON
    (result1.pernr = result2.pernr) AND
    (
        (DATEADD(day, -1, result1.[from]) = t3.till OR
        (DATEADD(day, -1, result1.[from]) = t2.till)
    ) AND
    result1.rn = result2.rn and
    result1.costcentre = result2.costcentre and
    result1.rownumber = result2.rownumber and
    result1.percentage = result2.percentage
;

-- Join using GROUP BY
SELECT t2.pernr, t2.name, min(t2.[from]) from, max(t2.till) till, t2.costcentre, t2.rownumber, t2.percentage
FROM #concatenated t2
GROUP BY t2.pernr, t2.name, t2.costcentre, t2.rownumber, t2.percentage;

-- Find and add all not yet matched
INSERT INTO #concatenated2
SELECT t1.pernr, t1.name, t1.[from], t1.till, t1.costcentre, t1.rownumber, t1.percentage
FROM #ergebnis t1
WHERE
    [from] not in (select [from] from #concatenated t2 where t1.pernr = t2.pernr)
    and till not in (select till from #concatenated t2 where t1.pernr = t2.pernr);

-- Show the result
SELECT * FROM #concatenated2;

这将 return 结果,但不是预期的结果。第一个语句 returns 仅包含 RowNumber == 3 的所有行。在第二条语句中,3 行将被压在一起,每个块的第一行和第二行没有 space。

有没有办法正确组合时间跨度? - 我正在使用 SQL Server 2014 标准版,因此 PIVOT 和 PARTITION 之类的东西可以工作。

试试这个:

SELECT PerNr, Name, MIN([FROM]) AS [FROM] ,MAX(Till) AS Till, CostCentre, RowNumber, Percentage
FROM #result
GROUP BY PerNr, Name, CostCentre,RowNumber,Percentage