sql server 2012 - Time difference between two rows for specified ID -


i'm trying find time difference in seconds between 2 rows have same id.

here's simple table.

enter image description here

the table ordered myid , timestamp. i'm trying total second between 2 rows have same myid.

here's have come with. problem query calculates time difference records not same id.

select  datediff(second, ptimestamp, timestamp), q.*    (             select  *,             lag(timestamp) on (order timestamp) ptimestamp                data         ) q   ptimestamp not null  

this output.

enter image description here

i want output highlighted in yellow.

any suggestions?

sqlfiddle

the fix matter of narrowing window, partition by, rows same id:

select  datediff(second, ptimestamp, timestamp), q.*    (             select  *,             lag(timestamp) on (partition id order timestamp) ptimestamp                data         ) q   ptimestamp not null  

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -