Question Locked

SQL previous record

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I would like to find the previous Speed to all cars in a database and compare it to the last speed it was changed by date. Then do a count of how many times it was changed. Can someone help? Thanks.

Example:

CarNum DorDate SpeedoEnd PrevEnd
16 5/1/2008 337374
16 5/2/2008 337538 337374
16 5/3/2008 337898 337538
16 5/4/2008 338212 337898
17 5/1/2008 201371
17 5/2/2008 201519 201371
17 5/3/2008 201784 201519
18 5/1/2008 196520
18 5/2/2008 196719 196520
18 5/3/2008 196774 196719

2 replies

by tony hopkinson

do
select Distinct CarNum,DORDate,SpeedoEnd
to get all the changes, Distinct would cut out any records where SpeedoEnd didn't change

and then count from that so

Select CarNum,Count(DORDate) From
(select Distinct CarNum,DORDate,SpeedoEnd) changes

changes is just a dummy name to fool sql into taking the query in brackets as a table

PrevEnd isn't helping you at all by the way, I'd try and get rid of it, it's a carry over from a non-db system

This discussion is closed to new replies.