天之间的价值差异

时间:2015-01-22 17:00:37

标签: sql-server date

我有一个名为" Journeys"的SQL Server 2005表。如下:

+---------------+
|   Journeys    |
+---------------+
| JourneyID     |
| PlateNumber   |
| DepartureDate |
| DepartureKM   |
| ArrivalDate   |
| ArrivalKM     |
+---------------+

该表包含以下示例数据:

+------------+--------------+----------------+--------------+--------------+-----------+
| JOURNEYID  | PLATENUMBER  | DEPARTUREDATE  | DEPARTUREKM  | ARRIVALDATE  | ARRIVALKM |
+------------+--------------+----------------+--------------+--------------+-----------+
|         1  | ABC-123      | 01-01-2015     |       10000  | 01-02-2015   |     10200 |
|         2  | ABC-123      | 01-02-2015     |       10210  | 01-03-2015   |     10500 |
|         3  | ABC-123      | 01-03-2015     |       10500  | 01-04-2015   |     10650 |
|         4  | ABC-123      | 01-04-2015     |       10607  | 01-05-2015   |     10900 |
|         5  | XYZ-999      | 01-15-2015     |       30200  | 01-16-2015   |     30400 |
|         6  | XYZ-999      | 01-16-2015     |       30405  | 01-17-2015   |     30600 |
|         7  | XYZ-999      | 01-17-2015     |       30600  | 01-18-2015   |     30750 |
|         8  | XYZ-999      | 01-18-2015     |       30752  | 01-19-2015   |     30920 |
+------------+--------------+----------------+--------------+--------------+-----------+

我想生成一个查询,该查询使用名为' KMDifference'的额外列返回以下结果。这是ArrivalKM'之间的区别。从最后一天起,' DepartureKM'从今天开始。

期望的结果:

+-------------+---------------+-------------+-------------+-----------+--------------+
| PlateNumber | DepartureDate | DepartureKM | ArrivalDate | ArrivalKM | KMDifference |
+-------------+---------------+-------------+-------------+-----------+--------------+
| ABC-123     | 01-01-2015    |       10000 | 01-02-2015  |     10200 |            0 |
| ABC-123     | 01-02-2015    |       10210 | 01-03-2015  |     10500 |           10 |
| ABC-123     | 01-03-2015    |       10500 | 01-04-2015  |     10650 |            0 |
| ABC-123     | 01-04-2015    |       10607 | 01-05-2015  |     10900 |            7 |
| XYZ-999     | 01-15-2015    |       30200 | 01-16-2015  |     30400 |            0 |
| XYZ-999     | 01-16-2015    |       30405 | 01-17-2015  |     30600 |            5 |
| XYZ-999     | 01-17-2015    |       30600 | 01-18-2015  |     30750 |            0 |
| XYZ-999     | 01-18-2015    |       30752 | 01-19-2015  |     30920 |            2 |
+-------------+---------------+-------------+-------------+-----------+--------------+

请在此处查看此SQL小提琴:http://sqlfiddle.com/#!3/28abd

1 个答案:

答案 0 :(得分:0)

您可以使用以下基于CTE的查询来模拟从SQL Server 2012开始提供的LAG功能:

;WITH Journeys_rn AS (
   SELECT JourneyID, PlateNumber, DepartureDate, DepartureKM, ArrivalDate, ArrivalKM,
          ROW_NUMBER() OVER(PARTITION BY PlateNumber ORDER BY DepartureDate) AS rn
   FROM Journeys
)
SELECT j1.PlateNumber, j1.DepartureDate, j1.DepartureKM,
       j1.ArrivalDate, j1.ArrivalKM,  
       j1.DepartureKM - ISNULL(j2.ArrivalKM, j1.DepartureKM) AS KMDifference
FROM Journeys_rn AS j1 
LEFT JOIN Journeys_rn AS j2 ON j1.rn = j2.rn+1 AND j1.PlateNumber = j2.PlateNumber
上述查询中的

j2.ArrivalKM是来自之前记录的值。需要ISNULL,以便为每个组的第一个记录计算0值。

SQL Fiddle Demo

P.S。 OP中的样本数据与OP中提供的SQL Fiddle源的样本数据不匹配。因此必须修复它以获得所需的reult集。