I had created the following view,
SELECT
currency_cd, SUM(comprate) AS salary, employee_code,
(SELECT SUM(c.comprate) AS Expr1
FROM dbo.employees_compensation AS c
INNER JOIN
(SELECT MAX(id) AS id, comp_ratecd
FROM dbo.employees_compensation
WHERE (effdt = (SELECT MAX(effdt) AS Expr1
FROM dbo.employees_compensation AS employees_compensation_1
WHERE (employee_code = b.employee_code)
)
) AND (employee_code = b.employee_code)
GROUP BY
comp_ratecd) AS a ON c.id = a.id) AS salary1
FROM
dbo.employees_compensation AS b
GROUP BY
currency_cd, employee_code
It is working fine and giving the expected results. But the problem is it takes time in executing and giving the results. And sometime it gives error also like Timeout etc.. Could you help me in solving the issue?
Maybe you should reorganize your DB? Create tables that will represents joins, for example.
If you don’t care about timing you can avoid timeout error by creating temprorary tables or virtual tables (aka views).
Avoid such a complex and nested expressions.