I need to perform a relatively simple explanation, but (given my somewhat limited skills) it is difficult to write an SQL query.
Suppose we have a table like this:
exam_no | name | surname | result | date ---------+------+---------+--------+------------ 1 | John | Doe | PASS | 2012-01-01 1 | Ryan | Smith | FAIL | 2012-01-02 <-- 1 | Ann | Evans | PASS | 2012-01-03 1 | Mary | Lee | FAIL | 2012-01-04 ... | ... | ... | ... | ... 2 | John | Doe | FAIL | 2012-02-01 <-- 2 | Ryan | Smith | FAIL | 2012-02-02 2 | Ann | Evans | FAIL | 2012-02-03 2 | Mary | Lee | PASS | 2012-02-04 ... | ... | ... | ... | ... 3 | John | Doe | FAIL | 2012-03-01 3 | Ryan | Smith | FAIL | 2012-03-02 3 | Ann | Evans | PASS | 2012-03-03 3 | Mary | Lee | FAIL | 2012-03-04 <--
Note that exam_no and date not necessarily related, as you might expect from the type I selected.
Now the query I need to do is as follows:
- In the final exam (
exam_no = 3), find all the students who failed ( John Doe , Ryan Smith and Mary Lee ). - For each of these students, find the date of the first in a series of successively failed exams. Another way would be: for each of these students to find the date of the first failed exam that comes after the last exam. (Look at the arrows in the table).
As a result, the table should look something like this:
name | surname | date_since_failing ------+---------+-------------------- John | Doe | 2012-02-01 Ryan | Smith | 2012-01-02 Mary | Lee | 2012-03-04
How can I fulfill such a request?
Thank you for your time.
sql database mysql
Gabriel
source share