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

Better and efficient way to write this query

$
0
0

Hello All,

I am asking whether there is a better way to write below query or not.Or could you give me some advices make it perform well ?

select id_emp,amount
  from (
		select id_emp,event_month,amount,ROW_NUMBER () over ( partition by id_emp order by event_month desc ) rn 
		  from (
					select id_emp,event_month,SUM(amount) amount
					  from Table_X 
					  where 1 = 1 
					    and id_emp = @id_emp 
					    and group_type = @group_type
					  group by id_emp,event_month
		) sub 
) sub 
 where sub.rn = 1 




Viewing all articles
Browse latest Browse all 23857

Trending Articles



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