I have a query that sums record from a column for a particular month based on username with latest record for particular user. But the query takes sometimes forever to execute. Can anybody suggest what I am doing wrong.
Select
SUM(t1.some_value)
from
tblDemo t1
where
t1.month='082017' and
t1.username='xyz' and
t1.table_no IN ('4a', '4b', '4c', '5a', '5b') and
t1.row_version = (
Select max(t2.row_version)
from tblDemo t2
where t2.inv_no = t1.inv_no
)
The table has approx 5 000 records for each month. What can I do to improve speed?