I have a table with date ranges (seasons) for each year. The normal is that the end of the one season is the beggining of the next season. In the example below I have in bold the two irregular season setups. In the first the end of season 1 is a day after the beggining of season 2. In the second, the beggining of season 4 is one day after the end of seaon 3
+--------+----+--------------+--------------+
|SEASONID|YEAR|DATE FROM |DATE TO |
+--------+----+--------------+--------------+
|1 |14 | 2014-01-01 |**2014-01-31**|
|2 |14 |**2014-01-30**| 2014-03-01 |
|3 |14 | 2014-03-01 |**2014-05-22**|
|4 |14 |**2014-05-23**| 2014-10-16 |
|5 |14 | 2014-10-16 | 2014-12-01 |
+--------+----+--------------+--------------+
Is there a way to write a query that can capture the seasons that are not correctly setup? (the ones that the end of one season is not the beginning of the next)
An ad-hoc brute-force approach:
Hopefully there are not enough records to cause performance issues with cartesian join. If you have SQL Server 2012 it can be improved using window functions (only compare adjacent seasons).
I hope this will help you
where stest having the following detail
and the above query will give the following result
from the result you can get id ( 1,2 ) and (3,4) are mismatched
and if you have do like below
then you will get the matching result
This answers half of your question: use the overlapping date queries from this article to find conflicting records:
Result:
SQL Fiddle