(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
(SELECT smmActivities.ACTIVITYNUMBER,
smmActivities.Purpose,
smmActivities.Closed,
smmActivities.Enddatetime,
smmActivities.ResponsibleEmployee,
SALESQUOTATIONTABLE.OPPORTUNITYID,
SALESQUOTATIONTABLE.QUOTATIONID,
row_number() over(partition by QUOTATIONID order by Closed, Enddatetime desc) as ord
FROM smmActivityParentLinkTable
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),
quotes as
(SELECT LEFT(QUOTATIONDOCNUM,11) as QuotationID,
Quotationdate,
Quotationamount,
CAST(RIGHT(QUOTATIONDOCNUM, LEN(QUOTATIONDOCNUM)-12) as INT) as Revision,
SALESADMINISTRATOR,
row_number() over(partition by LEFT(QUOTATIONDOCNUM,11) order by QUOTATIONDOCNUM desc) as ord,
SALESBALANCE
FROM CustQuotationJour
WHERE DATAAREAID ='ES75' and Year(Quotationdate)>2013),
opportunity as
(SELECT smmOpportunityTable.OPPORTUNITYID,
smmOpportunityTable.SUBJECT,
smmOpportunityTable.ESTIMATEDREVENUE,
smmOpportunityTable.OPENEDBY,
smmOpportunityTable.WK_RATINGVALUE,
smmOpportunityTable.WK_BUSRELACCOUNT,
smmOpportunityTable.WK_PROCESSSTAGENUMBER,
smmOpportunityTable.WK_PROGRESSPERCENTVALUE,
smmOpportunityTable.WK_QUALITYPERCENTVALUE,
smmOpportunityTable.WK_BUSINESSTYPEID,
smmOpportunityTable.PARTYID,
smmOpportunityTable.WK_CUSTACCOUNT,
smmOpportunityTable.WK_KEYBUYINGFACTORS,
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.QUOTATIONID,
SALESQUOTATIONTABLE.QUOTATIONNAME,
SALESQUOTATIONTABLE.ReceiptDateRequested,
SALESQUOTATIONTABLE.WK_SALESQUOTATIONTOTALAMOUNT,
SALESQUOTATIONTABLE.SalesResponsible as ASM,
SALESQUOTATIONTABLE.CreatedBY,
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
FROM smmOpportunityTable
left outer join SALESQUOTATIONTABLE on smmOpportunityTable.OPPORTUNITYID = SALESQUOTATIONTABLE.OPPORTUNITYID and SALESQUOTATIONTABLE.DATAAREAID = 'ES75'
WHERE smmOpportunityTable.DATAAREAID = 'ES75' and smmOpportunityTable.WK_BUSINESSTYPEID = 'EPC'),
COE as
(SELECT OPPORTUNITYID,
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
FROM WK_CRMBUDGETDETAILS
WHERE DATAAREAID = 'ES75'
GROUP BY OPPORTUNITYID)
SELECT opportunity.*,
activities.ACTIVITYNUMBER,
activities.Purpose,
activities.Closed,
activities.Enddatetime,
activities.ResponsibleEmployee,
quotes.Revision,
quotes.Quotationdate,
quotes.Salesbalance,
COALESCE(quotes.SALESADMINISTRATOR, opportunity.CreatedBY) as PM,
COE.COE
from opportunity
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.