24.4. Bound Queries

Another form of query asks whether there is an overall trend between two points in time bounded by a low value and a high value in the sequence of data. This is easier to show with an example. Let us assume that we have data on the selling prices of a stock in a table. We want to find periods of time when the price was generally increasing.

Consider this data:

MyStock
sale_date        price
======================
'2006-12-01'    10.00
'2006-12-02'    15.00
'2006-12-03'    13.00
'2006-12-04'    12.00
'2006-12-05'    20.00

The stock was generally increasing in all the periods that began on December 1 or ended on December 5—that is, it finished higher at the ends of those periods, in spite of the slump in the middle. A query for this problem is: ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.