Question

I am trying to work out how to to apply a datediff between rows where a rank is applied to the USER ID; Example of how the data below;

    UserID  Order Number    ScanDateStart   ScanDateEnd Minute Difference   Rank    |    Minute Difference Rank vs Rank+1
    User1      10-24        10:20:00        10:40:00        20                  1   |    5
    User1      10-25        10:45:00        10:50:00        5                   2   |    33
    User1      10-26        11:12:00        11:45:00        33                  3   |    NULL
    User2      10-10        00:09:00        00:09:20        20                  1   |    4
    User2      10-11        00:09:24        00:09:25        1                   2   |   15
    User2      10-12        00:09:40        00:10:12        32                  3   |    3
    User2      10-13        00:10:15        00:10:35        20                  4   |    NULL

What i'm looking for is how to code the final column of this table. The rank is applied to UserID ordered by ScanDateStart.

Basically, i want to know the time between the ScanDateEnd of Rank 1, to ScanDateStart of Rank2, and so on, but for each user.... (calculating time between order processing etc)

Appreciate the help

No correct solution

OTHER TIPS

This can be achieved by performing a LEFT JOIN to the same table on the UserID column and the Rank column, plus 1.

The following (simplified) pseudo-code should illustrate how to achieve this:

SELECT  R.UserID,
        R.Rank,
        R1.Diff
  FROM  Rank R
    LEFT JOIN Rank R1 ON R1.UserID = R.UserID AND R1.Rank = R.Rank + 1

Effectively, you are showing the UserID and Rank from the current row, but the Difference from the row of the same UserID with the Rank + 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top