A simple query to find missing data for date ranges in sql

Sometimes we have seemingly simple problems like, "we store our daily data in a table, how can I figure out, what dates the data is missing for.". This problem statement looks rather simple, and obvious solution that one may think can be to loop through the records and find out the missing dates in a defined interval.
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

If we compare the incremented and original series, we see that how "4" and "7" stand out. "10" is obviously out of bounds here.














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

Popular posts from this blog

Caused by: java.sql.SQLTimeoutException: ORA-01013: user requested cancel of current operation

HashiCorp Vault Integration with Ansible Etower using approle

utility to extract date from text with java