Ok, so here is the problem I have two tables in my SQL DB. Table 1 is a simple contact table with a record for each individual in my DB. Table 2 is an education/Exam table where there is a single record for each individual in the DB but each record has multiple tests in it. I am trying to query the two tables to get me a list of all records who have expired values(older than today) so I can flag the record for deletion. All dates stored in the DB are the expire dates. The kicker on this is that since an individuals exam records all exist in a single row for the record, if even one of the exam dates is not expired, the entire record needs to be kept. I can only delete a record if I have a combination of null values and expired dates. Not everyone has 4 records, most have one or two with the remaining records as Null. So here is my code to get the data:
select n.id, n.FULL_NAME, e.ExamDate1, e.ExamDate2, e.ExamDate3, e.ExamDate4
from Name n inner join Education e on n.id = e.id
This gets me 57k records and they are all a mix. A snippet of the results look like this:
| ID | FullName | ExamDate1 | ExamDatew | ExamDate3 | ExamDate4 |
| 10001 | John Doe | 1/1/2008 | Null | Null | 10/25/2015 |
| 10002 | Jane Doe | 10/8/2020 | 1/25/2015 | 4/16/2014 | Null |
| 10003 | John Smith | 1/1/2010 | 6/5/2008 | 9/15/2013 | 7/8/2001 |
| 10004 | Debbie Sue | 8/14/2020 | 6/5/2016 | Null | Null |
| 10005 | Suzy Q | 5/9/2016 | 4/15/2014 | 4/9/2017 | Null |
I need to be able to take this data and sort through it and flag each person on good or bad. Good if at least one listed date is not expired and bad if all listed dates are expired or all dates are expired and the records without dates are null.
Any ideas on what the easiest way to do this is? I was thinking a case statement in a procedure where I run through each record using a case for each exam date. Have a stored variable that starts as blank and add a value to that stored variable as variable = Variable + 'X' and at the end of the cycle, look at the value of the variable. If the value is yyyy then the record is flagged(shown) if not, then the record is left alone. Once the cycle is done, reset the variable to blank and start it over.
I hope this is making sense as it does in my head but not always on paper.
Thanks.