In general, there is a table with three fields: [name] nvarchar (50) null, [createdDate] date null, [sum] money null. It is required to select three employees to whom for a certain period of time paid the highest amount.
I know how to ask a request to choose the amount of payments for a certain time interval, for example:

select SUM([tst_temp].[sum]) as [сумма] from dbo.tst_temp where createdDate >= '19800101' and createdDate <= '20160801' and [name] = 'Иванов'; 

I know that I need to use TOR () and GROUP BY to sort and select the desired values. But how to make a final request or I do not understand ...
Help me please.

    1 answer 1

    Group by name.

     select TOP 3 [tst_temp].[name], SUM([tst_temp].[sum]) as [сумма] from dbo.tst_temp where createdDate >= '19800101' and createdDate <= '20160801' group by [tst_temp].[name] order by [сумма] desc 

    Something like this.

    • one
      Thanks privod , I did not expect that it is so easy. I thought already to adjust the cursor. The last line only [sum], not [tst_temp]. [Sum]. Thanks again for the help. - student007