sql server 2012: Reporting Query needs rethinking.

Gurus,
This query works---but crawls.  I have attached the query plan. The expense is in the Hash Match filter toward the return of the results.

	SELECT 
	   cmsp.[FleetCustomerId]
	  ,cmsp.[XactMonth] as [Month]
          ,cmsp.[XactYear] as [Year]
	  ,sum(cmsp.[TotalDieselAndRefrigeratorTransactionAmt]) as MonthlyRetailTotal
	  ,sum(cmsp.RetailPriceForFleetAtStore / sm.StoreCount) as MonthlyRetailAveragePrice
	  ,sum(cmsp.DiscountPriceForFleetAtStore / sm.StoreCount) as MonthlyRetailAverageDiscountPrice
	  ,sum(cmsp.[RefrigeratorAmount]) as MonthlyRetailRefrigTotal
	  ,sum(cmsp.[TransactionAmount]) as MonthlyRetailDieselTotal
	  ,sum(cmsp.[DiscountTotalDiesel]) as MonthlyDiscountDieselTotal
	  ,sum(cmsp.[TotalDieselAndRefrigeratorGallons]) as MonthlyTotalGallonsPurchased
  FROM CustomerPortalStaging.[integration].[ConsolidatedMonthStorePricing] cmsp
   inner join  #storemonthcounts sm
    on sm.XactYear = cmsp.[XactYear]
	 and sm.XactMonth = cmsp.[XactMonth]
  group by cmsp.FleetCustomerId,cmsp.XactYear,cmsp.XactMonth 

Open in new window


The temp table build just prior looks like this:

	 select count(*) StoreCount,
	        cmsp.XactYear,
			cmsp.XactMonth,
			cmsp.FleetCustomerId
			 into #StoreMonthCounts
	   from CustomerPortalStaging.integration.ConsolidatedMonthStorePricing cmsp
	    group by cmsp.fleetcustomerid,cmsp.xactyear, cmsp.xactmonth
     
create index IDX_temp_Month 
ON #StoreMonthCounts (XactMonth)

create index IDX_temp_Year
 on #StoreMonthCounts (XactYear)

create index IDX_temp_FleetCustomer
 on #StoreMonthCounts (FleetCustomerId)

Open in new window

PopulateReportingFleetMonthlySummary.sql
Paula DiTalloIntegration developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
Why use the temp table at all, doesn't seen to do much, Just add the count into the other portion of the query.

      SELECT
         cmsp.[FleetCustomerId]
        ,cmsp.[XactMonth] as [Month]
          ,cmsp.[XactYear] as [Year]

         , count(*) StoreCount

        ,sum(cmsp.[TotalDieselAndRefrigeratorTransactionAmt]) as MonthlyRetailTotal
        ,sum(cmsp.RetailPriceForFleetAtStore / sm.StoreCount) as MonthlyRetailAveragePrice
        ,sum(cmsp.DiscountPriceForFleetAtStore / sm.StoreCount) as MonthlyRetailAverageDiscountPrice
        ,sum(cmsp.[RefrigeratorAmount]) as MonthlyRetailRefrigTotal
        ,sum(cmsp.[TransactionAmount]) as MonthlyRetailDieselTotal
        ,sum(cmsp.[DiscountTotalDiesel]) as MonthlyDiscountDieselTotal
        ,sum(cmsp.[TotalDieselAndRefrigeratorGallons]) as MonthlyTotalGallonsPurchased
  FROM CustomerPortalStaging.[integration].[ConsolidatedMonthStorePricing] cmsp
  GROUP BY cmsp.FleetCustomerId,cmsp.XactYear,cmsp.XactMonth
1
Paula DiTalloIntegration developerAuthor Commented:
Paul,
I will look at your suggestion.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
ditallop, do you still need help with this question?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Paula DiTalloIntegration developerAuthor Commented:
Thanks everyone for  you help. I've solved it.
0
PortletPaulfreelancerCommented:
Great! So the question can be closed?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paula DiTalloIntegration developerAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.