Select rows until condition met

Use a sub-query to find out at what point you should stop, then return all row from your starting point to the calculated stop point.

SELECT
  *
FROM
  yourTable
WHERE
      id >= 4
  AND id <= (SELECT MIN(id) FROM yourTable WHERE b = 'F' AND id >= 4)

Note, this assumes that the last record is always an ‘F’. You can deal with the last record being a ‘T’ using a COALESCE.

SELECT
  *
FROM
  yourTable
WHERE
      id >= 4
  AND id <= COALESCE(
              (SELECT MIN(id) FROM yourTable WHERE b = 'F' AND id >= 4),
              (SELECT MAX(id) FROM yourTable                          )
            )

Leave a Comment