Using SqlServer 2008, I have a temp table (#tmpAllSales) that captures all sales for a year. I would like to create a SELECT statement that contains sales for the year as well as sales for the last quarter.
Something like this (@StartDate & @EndDate are defined as the start/end of a quarter):
--QTD Sales By Company
SELECT
Company,
SUM(Call) as TotalCallsQTD,
SUM(Call) - SUM(SoldCall) as FreeCallsQTD,
SUM(SalableCall) as SalesRelatedCallsQTD,
SUM(SoldCall) as SoldCallsQTD
FROM
#tmpAllSales
WHERE
CalledOn between @StartDate and @EndDate
GROUP BY
Company
--COMBINED WITH BASED ON Company
--YTD Sales By Company
SELECT
Company,
SUM(Call) as TotalCallsYTD,
SUM(Call) - SUM(SoldCall) as FreeCallsYTD,
SUM(SalableCall) as SalesRelatedCallsYTD,
SUM(SoldCall) as SoldCallsYTD
FROM
#tmpAllSales
GROUP BY
Company
ORDER BY
Company
Each separate query gives the results I want but I don’t know how to combine into one result set.
EDIT
I didn’t make it clear in my original post that I would like to get all the results on one row per company. I am using this in an Excel macro so it would be great if all the info was on one row.
If you want them as a single row for each Company, then: