I have two tables (SalesforceTasks and SalesforceContacts) that I am using for a scoring system project. A simple SELECT statement with a ROW\_NUMBER() calculation is taking a very long time to run and actually stops querying once it hits a certain number of rows. The query doesn't stop executing, but it stops returning data.

Here is the query in question. It is a very vanilla process, where I need to get the newest date in the SalesforceTasks table and link it to the contact ID in the SalesforceContacts table. The SalesforceTasks table has 2,091,946 rows and the SalesforceContacts table has 446,772 rows.

Here is the query in question:

```
SELECT SC.ID
      ,CASE
       WHEN DATEDIFF(DD, ST.CREATEDDATE, GETDATE()) BETWEEN 360 AND 1500
       THEN 15
       WHEN DATEDIFF(DD, ST.CREATEDDATE, GETDATE()) BETWEEN 181 AND 360
       THEN 10
       WHEN DATEDIFF(DD, ST.CREATEDDATE, GETDATE()) BETWEEN 60 AND 180
       THEN 5
       ELSE 0    
       END AS Score
      ,ROW_NUMBER() OVER (PARTITION BY ST.ACCOUNTID ORDER BY ACTIVITYDATE) AS LastCall 
  FROM Salesforce.dbo.SalesforceTasks AS ST
  JOIN Salesforce.dbo.SalesforceContacts AS SC
    ON ST.ACCOUNTID = SC.ACCOUNTID
 WHERE STATUS = 'Completed'
   AND TYPE LIKE 'Call%'

```

What is the best plan of attack here? As stated, the query is taking a very, very long time to run. Is there a better way to get the newest date from the SalesforceTasks table?