This query is meant to serve as a proof-of-concept for parsing sleep data off of the fitbit API, which returns you a start date, and then a series of times. Given that these data are polled every minute, you can safely assume that, when you cross a date boundary, the next measurement of time will be "smaller" than the last, and we could just break it into two days.
However, in an extreme edge case where either the fitbit is reporting wrong, or someone really is sleeping for more than 24 hours,