Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How do I optimize this simple query

$
0
0

Hi everyone,

I am using the following query in an application. Basically, the query shows account balances for the last 2 years. I need to get the same query to show data for the last 3 years. I can add another sub query and get the balances for the second prior year, but am thinking there is a better way to write this query as this one would result in 3 table scans. Any help would be appreciated.

SELECT E1.ID, E1.AccountName AS Account, E1.Q1, E1.Q2, E1.Q3
	   E2.Q1, E2.Q2, E2.Q3, E2.Q4 
FROM (SELECT * FROM [Employees] WHERE YEAR = YEAR(GETDATE())) E1
LEFT JOIN  (SELECT * FROM Employees] WHERE YEAR = YEAR(GETDATE())-1) E2 ON E1.ID = E2.ID 



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>