Let’s say a house gets painted every so often, and I track the changes (the ‘paint’ event) in a simple table:
_______________________
/ PaintDate | NewColor \
|-----------|-----------|
| 1/2/2012 | Red |
| 3/5/2013 | Blue |
| 9/9/2013 | Green |
\___________|___________/
Is there a SELECT statement that can give me a table of the date ranges a house was at a specific color?
Desired output:
_______________________________
/ From | To | Color \
|----------|----------|---------|
| 1/2/2012 | 3/5/2013 | Red |
| 3/5/2013 | 9/9/2013 | Blue |
| 9/9/2013 | NULL | Green | -- Not repainted yet, the date in the 'To' column should be NULL
\__________|__________|_________/
Well you can use this query
OUTPUT
Demo
The above query can be improved like this
Demo