I have a stored procedure that has optional parameters (pretty much all of them); if the parameter is not supplied, it needs to pull all data.
I have the following right now.
select *
from myTableird
where
ird.tIS like COALESCE(null, ird.tIS)
and ird.InventoryDate = COALESCE(null, ird.InventoryDate)
and ird.DateClosed = COALESCE(null, ird.DateClosed)
This runs very quickly, less than a second to generate the results.
I need to append code to only pull only the most recent record, for each repID. When I add the following code (last and statement), it just runs forever and times out after a few minutes. what did I do wrong? please help optimize the query, if possible, please explain so that I can learn, going forward. thank you!
select *
from myTableird
where
ird.repID like COALESCE(null, ird.repID)
and ird.InventoryDate = COALESCE(null, ird.InventoryDate)
and ird.DateClosed = COALESCE(null, ird.DateClosed)
and ird.PeriodID in (SELECT MAX(PeriodID)
FROM myTableird
GROUP BY repID)