Possible Duplicate:
SQL: finding longest date gap
I’ve got a table of below structure:
| LearnerId | FirstName | LastName | EnrollmentDate | CompletionDate |
I need to get the longest period which the school has gone without enrolling or completing learners, using a SQL query. How do I do this?
I’ll assume you have access to CTEs and
ROW_NUMBER().First, you need an ordered list of the dates. That is, not two columns, but one. Then you can compare one date to the immediate next date quite simply.
As you have the data in two columns, creating this one ordered list will be relatively expensive. I hope for your sake that you don’t have a huge volume of data.