Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Values differences between days

Author  Topic 

milo1981
Starting Member

18 Posts

Posted - 2015-01-22 : 13:00:10


I have a SQL Server 2005 table titled "Journeys" as follows:

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

The table contains the following sample data:

+------------+--------------+----------------+--------------+--------------+-----------+
| 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 |
+------------+--------------+----------------+--------------+--------------+-----------+

I want to generate a query that returns a the following results with an extra column named 'KMDifference' which is the difference between 'ArrivalKM' from last day and 'DepartureKM' from today.

Desired results:

+-------------+---------------+-------------+-------------+-----------+--------------+
| 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 |
+-------------+---------------+-------------+-------------+-----------+--------------+

See this SQL Fiddle here: http://sqlfiddle.com/#!3/28abd

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-01-22 : 13:24:40
I don't understand the calculation. How did you get 10 for 01/02 and 01/03?

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

milo1981
Starting Member

18 Posts

Posted - 2015-01-22 : 14:11:53
quote:
Originally posted by tkizer

I don't understand the calculation. How did you get 10 for 01/02 and 01/03?

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/



Hello,

From this two lines?
| 1 | ABC-123 | 01-01-2015 | 10000 | 01-02-2015 | 10200 |
| 2 | ABC-123 | 01-02-2015 | 10210 | 01-03-2015 | 10500 |

Subtract 10210 - 10200 = 10

DepartureKM (second line) - ArrivalKM (first line)
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2015-01-22 : 15:10:58
Try this:
with cte
as (select *
,row_number() over(partition by PlateNumber order by DepartureDate,JourneyID) as rn
from Journeys
)
select a.PlateNumber
,a.DepartureDate
,a.DepartureKM
,a.ArrivalDate
,a.ArrivalKM
,isnull(a.DepartureKM-b.ArrivalKM,0) as KMDifference
from cte as a
left outer join cte as b
on b.PlateNumber=a.PlateNumber
and b.rn=a.rn-1
order by a.PlateNumber
,a.rn
However difference between journeyid 3 and 4 is -43 km (vehicle going reverse?).
Also the departure km on journeyid 6 and 7 from your sample data described here, is swapped in your link (sqlfiddle).
Go to Top of Page

milo1981
Starting Member

18 Posts

Posted - 2015-01-22 : 15:26:51
quote:
Originally posted by bitsmed

Try this:
with cte
as (select *
,row_number() over(partition by PlateNumber order by DepartureDate,JourneyID) as rn
from Journeys
)
select a.PlateNumber
,a.DepartureDate
,a.DepartureKM
,a.ArrivalDate
,a.ArrivalKM
,isnull(a.DepartureKM-b.ArrivalKM,0) as KMDifference
from cte as a
left outer join cte as b
on b.PlateNumber=a.PlateNumber
and b.rn=a.rn-1
order by a.PlateNumber
,a.rn
However difference between journeyid 3 and 4 is -43 km (vehicle going reverse?).
Also the departure km on journeyid 6 and 7 from your sample data described here, is swapped in your link (sqlfiddle).



Thanks, bitsmed! It works! There are a few typos in my data. Sorry about that.
Go to Top of Page
   

- Advertisement -