Normally I would do
select top (1) * from table where id active = '1' and doc_type_id = '404'
to get the latest row.
However I’m using IN to get results back. Plus a join.
select t1.*
from table1 t1 left join table2 t2 on (t1.propid = t2.propid)
where t2.caseid in ('100','101') and t1.active = '1' and b.doc_type_id = '404'
This query here returns the documents. However there are multiple documents of the same type.
And if each one has 10 docs I need only the latest one.
I would want to retrieve only the latest 1 doc for each from the database.
I hope I explained myself 🙂
Without seeing the return types you are going for, it sounds like you need to use a
GROUP BYand/or aMAX. If that does not help solve your problem, could you please post a sample data set and what you expect?You might even be able to use partitioning and/or ROW_NUMBER()
UPDATE BASED ON COMMENTS
This MIGHT work for you based on your comments