If I have a program in a repair shop and I want to select all the cars in my RepairOrder table, where the mileage of a later repair order is less than the mileage of a previous repair order, how can I create this selection statement?
ID VehicleID Mileage RepairDate 01 1 18425 2013-08-13 02 1 28952 2013-02-26 03 2 22318 2012-08-27 04 3 21309 2012-08-07 05 3 16311 2012-02-27 06 3 16310 2012-02-11 07 4 11098 2011-03-23 08 5 21309 2012-08-07 09 5 16309 2012-02-27 10 5 16310 2012-02-11
In this case, I should only select VehicleID 1, because it has a RepairDate that is larger than the previous row, but mileage that is less than the previous row. There may also be 3 rows with the same car, and the average date has a mileage of 3 or 5,000,000, and I will also need to select this VehicleID.
Results of using the LEAD () function
ID RepairDate Mileage 25 2011-12-23 45934 48 2009-02-26 13 48 2009-04-24 10 71 2011-07-26 31163 71 2015-01-13 65656
source share