连接多行时间范围

时间:2016-01-25 09:42:33

标签: sql sql-server sql-server-2014

在SQL Server上,我得到了一个返回以下员工数据的视图。如您所见,它显示了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;

这将返回结果,但不会返回预期的结果。第一个语句仅返回RowNumber == 3的所有行。在第二个语句中,3行将被压扁在一起,不会为每个块的第一行和第二行留出空间。

有没有办法正确组合时间跨度? - 我正在使用SQL Server 2014标准版,所以像PIVOT和PARTITION这样的东西都能正常工作。

1 个答案:

答案 0 :(得分:0)

试试这个:

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