Hello guys i had seprate 12 queries and some c# code to get the data formatted on the page, but now we are converted those pages to the SSRS reports. I have built the stored procedure which is combination of the 12 queries and some new additional queries to format the data as we want on the ssrs report. Now the new stored procedure is taking more time than the old page here is my query can any optimization possible on the following stored procedure any help would be great .
CREATE PROCEDURE [dbo].[GetHistoryByYear_Get]
-- Add the parameters for the stored procedure here
#Year AS VARCHAR(4),
#PreYear AS VARCHAR(4)
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT *
INTO #tempCustVol
FROM
(
SELECT * FROM
(
SELECT * FROM (
SELECT #Year AS 'Year',Company, Customer,SUM(Jan) AS Jan, SUM(Feb) AS Feb, SUM(Mar) As Mar, SUM(Apr) AS Apr, SUM(May) AS May, SUM(Jun) AS Jun, SUM(Jul) AS Jul, SUM(Aug) AS Aug, SUM(Sep) AS Sep, SUM(Oct) AS Oct, SUM(Nov) AS Nov, SUM(Dec) AS Dec
,(SUM(Jan) + SUM(Feb) + SUM(Mar) + SUM(Apr) + SUM(May) + SUM(Jun) + SUM(Jul) + SUM(Aug) + SUM(Sep) + SUM(Oct) + SUM(Nov) + SUM(Dec) ) AS YearlyTotal
FROM(
SELECT Company, Customer, DateRcvd, SUM(Records) AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '01' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, SUM(RECORDS) AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '02' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, SUM(RECORDS) As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '03' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, SUM(RECORDS) As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '04' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, SUM(RECORDS) As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '05' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, SUM(RECORDS) As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '06' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, SUM(RECORDS) As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '07' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, SUM(RECORDS) As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '08' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, SUM(RECORDS) As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '09' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, SUM(RECORDS) As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '10' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, SUM(RECORDS) As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '11' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, SUM(RECORDS) As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '12' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
) F
GROUP BY Company, Customer
) AS ALLDATA
UNION
SELECT * FROM (
SELECT #PreYear AS 'Year',Company, Customer,SUM(Jan) AS Jan, SUM(Feb) AS Feb, SUM(Mar) As Mar, SUM(Apr) AS Apr, SUM(May) AS May, SUM(Jun) AS Jun, SUM(Jul) AS Jul, SUM(Aug) AS Aug, SUM(Sep) AS Sep, SUM(Oct) AS Oct, SUM(Nov) AS Nov, SUM(Dec) AS Dec
,(SUM(Jan) + SUM(Feb) + SUM(Mar) + SUM(Apr) + SUM(May) + SUM(Jun) + SUM(Jul) + SUM(Aug) + SUM(Sep) + SUM(Oct) + SUM(Nov) + SUM(Dec) ) AS YearlyTotal
FROM(
SELECT Company, Customer, DateRcvd, SUM(Records) AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '01' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, SUM(RECORDS) AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '02' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, SUM(RECORDS) As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '03' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, SUM(RECORDS) As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '04' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, SUM(RECORDS) As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '05' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, SUM(RECORDS) As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '06' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, SUM(RECORDS) As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '07' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, SUM(RECORDS) As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '08' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, SUM(RECORDS) As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '09' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, SUM(RECORDS) As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '10' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, SUM(RECORDS) As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '11' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, SUM(RECORDS) As Dec
FROM(
select * from vwjmrep where datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND SUBSTRING(DateRcvd,5,2) = '12' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
) F
GROUP BY Company, Customer
) AS ALLDATA
) AS TEMPDATA
) AS data
SELECT * FROM (SELECT * FROM #tempCustVol
UNION
SELECT
#PreYear AS [Year],null,null,COALESCE(SUM(Jan),0),COALESCE(SUM(Feb),0),COALESCE(SUM(Mar),0),COALESCE(SUM(Apr),0),
COALESCE(SUM(May),0),COALESCE(SUM(Jun),0),COALESCE(SUM(Jul),0),COALESCE(SUM(Aug),0),COALESCE(SUM(Sep),0),COALESCE(SUM(Oct),0),
COALESCE(SUM(Nov),0),COALESCE(SUM(Dec),0),COALESCE((SUM(Jan) + SUM(Feb) + SUM(Mar) + SUM(Apr) + SUM(May) + SUM(Jun) + SUM(Jul) + SUM(Aug) + SUM(Sep) + SUM(Oct) + SUM(Nov) + SUM(Dec) ),0) AS YearlyTotal
FROM #tempCustVol
WHERE [Year] = #PreYear
)AS DA
ORDER BY
CASE WHEN Company is null THEN 1 ELSE 0 END,
Company,[Year]
DROP TABLE #tempCustVol
END
any help would be great i have indexed the tables and tables have lots of data it takes near about the 10 to 12 min to execute is there any way i can minimize it. and it's the SQL SERVER 2008 database
UPDATE
This is my updated stored procedure
BEGIN
DECLARE #Year AS VARCHAR(4),
#PreYear AS VARCHAR(4)
SET #Year='2013'
SET #PreYear='2012'
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT *
INTO #tempCustVol
FROM
(
SELECT * FROM
(
SELECT * FROM (
SELECT #Year AS 'Year',Company, Customer,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '01' THEN Records ELSE 0 END) AS Jan,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '02' THEN Records ELSE 0 END) AS Feb,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '03' THEN Records ELSE 0 END) AS Mar,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '04' THEN Records ELSE 0 END) AS Apr,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '05' THEN Records ELSE 0 END) AS May,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '06' THEN Records ELSE 0 END) AS Jun,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '07' THEN Records ELSE 0 END) AS Jul,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '08' THEN Records ELSE 0 END) AS Aug,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '09' THEN Records ELSE 0 END) AS Sep,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '10' THEN Records ELSE 0 END) AS Oct,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '11' THEN Records ELSE 0 END) AS Nov,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '12' THEN Records ELSE 0 END) AS Dec,
SUM(Records) AS YearlyTotal
FROM vwjmrep
WHERE datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND Company IS NOT NULL
GROUP BY Company, Customer
) AS ALLDATA
UNION ALL
SELECT * FROM (
SELECT #PreYear AS 'Year',Company, Customer,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '01' THEN Records ELSE 0 END) AS Jan,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '02' THEN Records ELSE 0 END) AS Feb,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '03' THEN Records ELSE 0 END) AS Mar,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '04' THEN Records ELSE 0 END) AS Apr,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '05' THEN Records ELSE 0 END) AS May,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '06' THEN Records ELSE 0 END) AS Jun,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '07' THEN Records ELSE 0 END) AS Jul,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '08' THEN Records ELSE 0 END) AS Aug,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '09' THEN Records ELSE 0 END) AS Sep,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '10' THEN Records ELSE 0 END) AS Oct,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '11' THEN Records ELSE 0 END) AS Nov,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '12' THEN Records ELSE 0 END) AS Dec,
SUM(Records) AS YearlyTotal
FROM vwjmrep
WHERE datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND Company IS NOT NULL
GROUP BY Company, Customer
) AS ALLDATA
) AS TEMPDATA
) AS data
SELECT * FROM (SELECT * FROM #tempCustVol
UNION ALL
SELECT
#PreYear AS [Year],null,null,COALESCE(SUM(Jan),0),COALESCE(SUM(Feb),0),COALESCE(SUM(Mar),0),COALESCE(SUM(Apr),0),
COALESCE(SUM(May),0),COALESCE(SUM(Jun),0),COALESCE(SUM(Jul),0),COALESCE(SUM(Aug),0),COALESCE(SUM(Sep),0),COALESCE(SUM(Oct),0),
COALESCE(SUM(Nov),0),COALESCE(SUM(Dec),0),COALESCE((SUM(Jan) + SUM(Feb) + SUM(Mar) + SUM(Apr) + SUM(May) + SUM(Jun) + SUM(Jul) + SUM(Aug) + SUM(Sep) + SUM(Oct) + SUM(Nov) + SUM(Dec) ),0) AS YearlyTotal
FROM #tempCustVol
WHERE [Year] = #PreYear
)AS DA
ORDER BY
CASE WHEN Company is null THEN 1 ELSE 0 END,
Company,[Year]
DROP TABLE #tempCustVol
END
which is still takes time but is there any more optimization possible thanks
I think, you can try replace this
SELECT * FROM (
SELECT #Year AS 'Year',Company, Customer,SUM(Jan) AS Jan, SUM(Feb) AS Feb, SUM(Mar) As Mar, SUM(Apr) AS Apr, SUM(May) AS May, SUM(Jun) AS Jun, SUM(Jul) AS Jul, SUM(Aug) AS Aug, SUM(Sep) AS Sep, SUM(Oct) AS Oct, SUM(Nov) AS Nov, SUM(Dec) AS Dec
,(SUM(Jan) + SUM(Feb) + SUM(Mar) + SUM(Apr) + SUM(May) + SUM(Jun) + SUM(Jul) + SUM(Aug) + SUM(Sep) + SUM(Oct) + SUM(Nov) + SUM(Dec) ) AS YearlyTotal
FROM(
SELECT Company, Customer, DateRcvd, SUM(Records) AS Jan, 0 AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '01' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
UNION
SELECT Company, Customer, DateRcvd, 0 AS Jan, SUM(RECORDS) AS Feb, 0 As Mar, 0 As Apr, 0 As May, 0 As Jun, 0 As Jul, 0 As Aug, 0 As Sep, 0 As Oct, 0 As Nov, 0 As Dec
FROM(
select * from vwjmrep where datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND SUBSTRING(DateRcvd,5,2) = '02' AND Company IS NOT NULL
) AS T
GROUP BY Company, Customer, DateRcvd
GROUP BY Company, Customer
with this
SELECT #Year AS 'Year',Company, Customer,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '01' THEN Records ELSE 0 END) AS Jan,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '02' THEN Records ELSE 0 END) AS Feb,
...,
SUM(Records) AS YearlyTotal
FROM vwjmrep
WHERE datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND Company IS NOT NULL
GROUP BY Company, Customer
At least, there will be less Table Scans.
Check this out
CREATE PROCEDURE [dbo].[GetHistoryByYear_Get]
-- Add the parameters for the stored procedure here
#Year AS VARCHAR(4),
#PreYear AS VARCHAR(4)
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT *
INTO #tempCustVol
FROM
(
SELECT *
, Jan + Feb + Mar + Apr + May + Jun + Jul + Aug + Sep + Oct + Nov + [Dec] AS YearlyTotal
FROM (
SELECT #Year AS 'Year', Company, Customer,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '01' THEN Records ELSE 0)) Jan,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '02' THEN Records ELSE 0)) Feb,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '03' THEN Records ELSE 0)) Mar,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '04' THEN Records ELSE 0)) Apr,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '05' THEN Records ELSE 0)) May,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '06' THEN Records ELSE 0)) Jun,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '07' THEN Records ELSE 0)) Jul,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '08' THEN Records ELSE 0)) Aug,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '09' THEN Records ELSE 0)) Sep,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '10' THEN Records ELSE 0)) Oct,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '11' THEN Records ELSE 0)) Nov,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '12' THEN Records ELSE 0)) [Dec]
FROM vwjmrep
WHERE Company IS NOT NULL
AND (datercvd >=#Year + '0101' AND datercvd <= #Year + '1231')
GROUP BY Company, Customer
UNION ALL
SELECT #PreYear AS 'Year', Company, Customer,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '01' THEN Records ELSE 0)) Jan,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '02' THEN Records ELSE 0)) Feb,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '03' THEN Records ELSE 0)) Mar,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '04' THEN Records ELSE 0)) Apr,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '05' THEN Records ELSE 0)) May,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '06' THEN Records ELSE 0)) Jun,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '07' THEN Records ELSE 0)) Jul,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '08' THEN Records ELSE 0)) Aug,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '09' THEN Records ELSE 0)) Sep,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '10' THEN Records ELSE 0)) Oct,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '11' THEN Records ELSE 0)) Nov,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '12' THEN Records ELSE 0)) [Dec]
FROM vwjmrep
WHERE Company IS NOT NULL
AND (datercvd >=#PreYear + '0101' AND datercvd <= #PreYear + '1231')
GROUP BY Company, Customer
) x
) ALLDATA
...Rest of the code here
about my last comment:- I still go by it.Doing some stuff in sql and some in C# specially when query is so heavy .also your query do not have pagination.So no optmize query can help if it more than 100 rows.Also you hv to use index.
Ok man check this out,if its not running then tell
;With CTE as
(SELECT #Year AS 'Year',Company, Customer,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '01' THEN Records ELSE 0 END) AS Jan,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '02' THEN Records ELSE 0 END) AS Feb,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '03' THEN Records ELSE 0 END) AS Mar,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '04' THEN Records ELSE 0 END) AS Apr,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '05' THEN Records ELSE 0 END) AS May,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '06' THEN Records ELSE 0 END) AS Jun,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '07' THEN Records ELSE 0 END) AS Jul,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '08' THEN Records ELSE 0 END) AS Aug,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '09' THEN Records ELSE 0 END) AS Sep,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '10' THEN Records ELSE 0 END) AS Oct,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '11' THEN Records ELSE 0 END) AS Nov,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '12' THEN Records ELSE 0 END) AS Dec,
SUM(Records) AS YearlyTotal
FROM vwjmrep
WHERE datercvd >=#Year + '0101' and datercvd <= #Year + '1231' AND Company IS NOT NULL
GROUP BY Company, Customer
Union All
SELECT #PreYear AS 'Year',Company, Customer,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '01' THEN Records ELSE 0 END) AS Jan,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '02' THEN Records ELSE 0 END) AS Feb,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '03' THEN Records ELSE 0 END) AS Mar,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '04' THEN Records ELSE 0 END) AS Apr,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '05' THEN Records ELSE 0 END) AS May,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '06' THEN Records ELSE 0 END) AS Jun,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '07' THEN Records ELSE 0 END) AS Jul,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '08' THEN Records ELSE 0 END) AS Aug,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '09' THEN Records ELSE 0 END) AS Sep,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '10' THEN Records ELSE 0 END) AS Oct,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '11' THEN Records ELSE 0 END) AS Nov,
SUM(CASE WHEN SUBSTRING(DateRcvd,5,2) = '12' THEN Records ELSE 0 END) AS Dec,
SUM(Records) AS YearlyTotal
FROM vwjmrep
WHERE datercvd >=#PreYear + '0101' and datercvd <= #PreYear + '1231' AND Company IS NOT NULL
GROUP BY Company, Customer
)
,CTE1 as
(Select * from cte
union all
SELECT
#PreYear AS [Year],null,null,COALESCE(SUM(Jan),0),COALESCE(SUM(Feb),0),COALESCE(SUM(Mar),0),COALESCE(SUM(Apr),0),
COALESCE(SUM(May),0),COALESCE(SUM(Jun),0),COALESCE(SUM(Jul),0),COALESCE(SUM(Aug),0),COALESCE(SUM(Sep),0),COALESCE(SUM(Oct),0),
COALESCE(SUM(Nov),0),COALESCE(SUM(Dec),0),COALESCE((SUM(Jan) + SUM(Feb) + SUM(Mar) + SUM(Apr) + SUM(May) + SUM(Jun) + SUM(Jul) + SUM(Aug) + SUM(Sep) + SUM(Oct) + SUM(Nov) + SUM(Dec) ),0) AS YearlyTotal
FROM cte
WHERE [Year] = #PreYear
)
select * from cte1
ORDER BY
CASE WHEN Company is null THEN 1 ELSE 0 END,
Company,[Year]