I have student documents table that store data with class and stream foreign id as following.
I want to fetch latest document from student_documents that is grouped by class and stream.
Current query and result is as below.
SELECT this.* FROM (
SELECT * from student_documents
where status != 'D'
order by document_id desc ) this
WHERE 1 AND(this.status!='D')
group by fk_class_id, fk_stream_id
order by document_id desc
limit 0,20
Here Iam expecting document_id 2,3 and 5 and getting 1,3 and 4.