How can I return the date difference in days between a customers (grouped by custno) sale dates? For example in the table below the first customer sale will have 0 date_diff in days as it is the first sale. However how do I return the date diff in subsequent sales dates? I'm looking to generate something like:
OK, for SQL 2000... And shamelessly nicking @Kev Riley's code...
You'll need to change the "sale_date" field from
which gives your final query (with date_diff):
Hope this helps.
answered Feb 15 at 11:47 AM
If you are on SQL 2012 or later, then you could use the LAG function to get the previous sale_date.
This query shows what values you get
and then you can use this to do the DATEDIFF
to give the results