Given this example data set:
-----------------------------
| item | date | val |
-----------------------------
| apple | 2012-01-11 | 15 |
| apple | 2012-02-12 | 19 |
| apple | 2012-03-13 | 7 |
| apple | 2012-04-14 | 6 |
| orange | 2012-01-11 | 15 |
| orange | 2012-02-12 | 8 |
| orange | 2012-03-13 | 11 |
| orange | 2012-04-14 | 9 |
| peach | 2012-03-13 | 5 |
| peach | 2012-04-14 | 15 |
-----------------------------
I’m looking for the query that for each item,
it will select the first date where the val went below CONST=10 without coming back above afterwards. In this example that would be:
-----------------------------
| item | date | val |
-----------------------------
| apple | 2012-03-13 | 7 |
| orange | 2012-04-14 | 9 |
-----------------------------
Is this even possible without using cursors? I’m looking for this in Sybase.
If this is not possible without cursors, I can process the records in a programming language. In that case however, since in my real use case the full history is very long, I need a “suitable” query that selects just “enough” records for computing the record I am ultimately after: the most recent record where val dipped below CONST without coming back above it.
This returns the result set as detailed.