I'm trying to select multiple rows where the value is closest to the end date but not passed, and return only those rows. Here's a sample of some data:
School Code | School Year | Semester | Term | Term End
========================================================
EXAMPLE | 20142015 | 1 | 1 | 2015-01-23
EXAMPLE | 20142015 | 1 | 2 | 2015-06-20
DEMO | 20142015 | 1 | 1 | 2015-01-23
DEMO | 20142015 | 1 | 2 | 2015-06-20
What I've done in my test case was:
SELECT TOP 1 * FROM School_Terms
WHERE Term_End >= '2015-01-20'
The problem is this only returns one row and not the first valid row for each school.
My expected results for the term end date '2015-01-20' are:
School Code | School Year | Semester | Term | Term End
========================================================
EXAMPLE | 20142015 | 1 | 1 | 2015-01-23
DEMO | 20142015 | 1 | 1 | 2015-01-23
and if the Term End was '2015-01-30' I would expect:
School Code | School Year | Semester | Term | Term End
========================================================
EXAMPLE | 20142015 | 1 | 2 | 2015-06-20
DEMO | 20142015 | 1 | 2 | 2015-06-20