A simple query to find missing data for date ranges in sql
But writing this logic in plain SQL is hard and one may try to find solace in some scripting language.
Can there be some other elegant solution?
Luckily there is an elegant trick, which is fast and only a few inches when you write:
Consider following sequence of integers as dates. (Easy to visualize the solution with small digits than dates)
1 |
2 |
3 |
5 |
6 |
8 |
9 |
We can see that if this is a series, we have missed digits "4" and "7".
Problem is how to determine this using a computer and not human brains.
Lets increment, every digit by one as in the following series.
1 + 1 = 2 |
2 + 1 = 3 |
3 + 1 = 4 |
5 + 1 = 6 |
6 + 1 = 7 |
8 + 1 = 9 |
9 + 1 =10 |
We can easily write a SQL query using this logic for our solution, assuming dates instead of numbers.
SELECT t2.date_col +1 as Missing_Date FROM table t1 LEFT OUTER JOIN table t2 ON t1.date_col +1 = t2.date_col WHERE t2.date_col is null
Comments
Post a Comment