I was hoping someone could steer me in the right direction with this one. I have a table that has student grades.
Create Table StudentGrades
(
StudentID references Student(StudentID) int not null,
GradeIssued varchar(1) not null,
WhenIssued datetime not null default(getdate())
)
I'd like to write a query that does something like: Find students who have grades (in sequence) of a D, then a C, then another C, then an A. I know the direction (because of the date the grades are issued) that the pattern should be in, but I can't think of how to write this without using a cursor.
In my experience with querying I'm filtering through via WHERE and HAVING clauses, but I'm not sure where to go when I want to find a particular pattern.
Best Answer
One way of doing it would be to use
LEAD
.Another way would be to use
XML PATH
to concatenate all grades in order andLIKE
.