I have multiple applications each with unique application ID’s, Each application has logs which are stored in a table. I want to know how to calculate the date difference
eg:
AppID Start Loggedon change
A1 08/07/2010 08/09/2010 Xchange
A1 08/07/2010 08/20/2010 Ychange
A1 08/07/2010 08/30/2010 Zchange
A2 07/07/2010 07/13/2010 Ychange
A3 09/07/2010 09/09/2010 Xchange
So I want the values
Difference
2 (Difference between the application start date and 1st loggedon date)
11 (difference between 08/20 and 08/09, the prior row because AppID stayed the same)
10 (difference between 08/30 and 08/20, the prior row because AppID stayed the same)
6 (Difference between the application start date and 1st loggedon date)
2 (Difference between the application start date and 1st loggedon date)
Hope I am clear. How can I achieve this, I tried Ranking and Row_Number. But I might be wrong somewhere. I am using SQL Server and cannot use LAG()
@Hogan has the correct approach, but for some reason, I can’t get it to work completely. Here is a slight variation that seems to produce the correct result — however, please accept @Hogan’s answer as he beat me to it 🙂
And here is the Fiddle.
Good luck.