(52 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'SMMACTIVITIES'. Scan count 0, logical reads 5423, physical reads 0, read-ahead reads 81, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'SALESQUOTATIONTABLE'. Scan count 52, logical reads 957228, physical reads 18, read-ahead reads 6800, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'SMMACTIVITYPARENTLINKTABLE'. Scan count 52, logical reads 28912, physical reads 1, read-ahead reads 236, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'SMMOPPORTUNITYTABLE'. Scan count 53, logical reads 316, physical reads 6, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'CUSTQUOTATIONJOUR'. Scan count 52, logical reads 400192, physical reads 2, read-ahead reads 199, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'WK_CRMBUDGETDETAILS'. Scan count 68, logical reads 140, physical reads 0, read-ahead reads 6, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 7800 ms, elapsed time = 12419 ms.
Here goes the query:
with activities as
row_number() over(partition by QUOTATIONID order by Closed, Enddatetime desc) as ord
left outer join smmActivities on smmActivityParentLinkTable.ACTIVITYNUMBER = smmActivities.ACTIVITYNUMBER and smmActivities.DATAAREAID = 'ES75'
inner join SALESQUOTATIONTABLE on smmActivityParentLinkTable.REFRECID = SALESQUOTATIONTABLE.RECID and SALESQUOTATIONTABLE.DATAAREAID = 'ES75'
inner join smmOpportunityTable on SALESQUOTATIONTABLE.OPPORTUNITYID = smmOpportunityTable.OPPORTUNITYID and smmOpportunityTable.DATAAREAID = 'ES75'
WHERE smmActivityParentLinkTable.DATAAREAID = 'ES75' and smmActivityParentLinkTable.REFTABLEID = '1967' and year(smmActivities.createddatetime) >2013),
(SELECT LEFT(QUOTATIONDOCNUM,11) as QuotationID,
CAST(RIGHT(QUOTATIONDOCNUM, LEN(QUOTATIONDOCNUM)-12) as INT) as Revision,
row_number() over(partition by LEFT(QUOTATIONDOCNUM,11) order by QUOTATIONDOCNUM desc) as ord,
WHERE DATAAREAID ='ES75' and Year(Quotationdate)>2013),
CASE WHEN smmOpportunityTable.Status = 1 THEN 'active'
WHEN smmOpportunityTable.Status = 2 THEN 'postponed'
WHEN smmOpportunityTable.Status = 3 THEN 'confirmed'
WHEN smmOpportunityTable.Status = 4 THEN 'cancelled'
WHEN smmOpportunityTable.Status = 5 THEN 'lost' ELSE 'shit happened' END as Status,
SALESQUOTATIONTABLE.SalesResponsible as ASM,
CASE WHEN SALESQUOTATIONTABLE.QUOTATIONSTATUS = 0 THEN 'Created'
WHEN SALESQUOTATIONTABLE.QUOTATIONSTATUS = 1 THEN 'Sent'
WHEN SALESQUOTATIONTABLE.QUOTATIONSTATUS = 2 THEN 'Confirmed'
WHEN SALESQUOTATIONTABLE.QUOTATIONSTATUS = 3 THEN 'Lost'
WHEN SALESQUOTATIONTABLE.QUOTATIONSTATUS = 4 THEN 'Cancelled' END as StatusQuo
left outer join SALESQUOTATIONTABLE on smmOpportunityTable.OPPORTUNITYID = SALESQUOTATIONTABLE.OPPORTUNITYID and SALESQUOTATIONTABLE.DATAAREAID = 'ES75'
WHERE smmOpportunityTable.DATAAREAID = 'ES75' and smmOpportunityTable.WK_BUSINESSTYPEID = 'EPC'),
stuff((select ', ' + a.PRODFAMILYGRPID
from WK_CRMBUDGETDETAILS a
WHERE a.DATAAREAID = 'ES75' and a.OPPORTUNITYID = WK_CRMBUDGETDETAILS.OPPORTUNITYID
for xml path('')),1,1,'') AS COE
WHERE DATAAREAID = 'ES75'
GROUP BY OPPORTUNITYID)
COALESCE(quotes.SALESADMINISTRATOR, opportunity.CreatedBY) as PM,
left outer join quotes on quotes.QuotationID = opportunity.QUOTATIONID and quotes.ord = 1
left outer join activities on opportunity.QUOTATIONID =activities.QUOTATIONID and activities.ord = 1
left outer join COE on opportunity.OPPORTUNITYID = COE.OPPORTUNITYID
ORDER BY ReceiptDateRequested desc
I don't believe in miracles either... that was just to say that I don't know too much about indexing. I know it's important for the performance but I could not change indexing anyway as I only have access rights to these databases. They belong to the ERP of our company so I presume they are correctly indexed.