sql server - Previous and Next row record -


i have following output

id   number   speed   lat           long           datetime 101   ab01      15    73.066016     33.5840768     9/1/15 23:10 101   ab01      20    73.0619712    33.5871456     9/1/15 23:30 101   ab01      0     73.0722176    33.6007488     9/1/15 23:45 101   ab01      0     73.0722112    33.6007488     9/2/15 02:10 101   ab01      0     73.0722176    33.6007008     9/2/15 02:35 101   ab01      0     73.0722432    33.6007456     9/2/15 04:35 101   ab01      0     73.0721664    33.6007904     9/3/15 12:35 101   ab01      0     73.072192     33.6007488     9/3/15 13:35 101   ab01      0     73.072192     33.6007488     9/4/15 11:35 101   ab01      0     73.072192     33.6007488     9/4/15 14:35 101   ab01      1     73.072192     33.6007488     9/5/15 14:35 

but required output

id   number   speed   lat           long           datetime 101   ab01      15    73.066016     33.5840768     9/1/15 23:10 101   ab01      20    73.0619712    33.5871456     9/1/15 23:30 101   ab01      0     73.0722176    33.6007488     9/1/15 23:45    101   ab01      0     73.072192     33.6007488     9/4/15 14:35 101   ab01      1     73.072192     33.6007488     9/5/15 14:35 

i want skip 0 speed value.if include these 0 zero's query response time increase. code attempt

select   distinct id,number,speed,lat,long,datetime        table group d,number,speed,lat,long,datetime  

you can use lead, lag window functions detect speed discontinuities (if want):

select id, number, speed, lat, [long], [datetime] (   select id, number, speed, lat, [long], [datetime],          lead(speed) on (partition id, number                            order [datetime]) nextspeed,          lag(speed) on (partition id, number                            order [datetime]) prevspeed            mytable) t speed <> coalesce(nextspeed, -1) or speed <> coalesce(prevspeed, -1) 

demo here


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -