Query optimization? - sql

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]

Related

How to count number of rows returned per month in oracle? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am creating a feature for my application where I need to generate a report for the whole 2018.
I need to count all the tickets for 2018. Each ticket has a category.
For example:
Change of name.
Senior Citizen etc.
I need to count the number of change name tickets, senior citizen tickets for 2018 per month
I tried to to this but I can't seem to get the result that I want.
I can't seem to break down the count per month.
This is the query that I have so far:
SELECT SUBCATEGORY,COUNT(ticket_no)
FROM CNR_TICKET
WHERE date_created >= TO_DATE('1/01/2018','MM/DD/YYYY')
AND date_created <= TO_DATE('12/31/2018','MM/DD/YYYY')
GROUP BY SUBCATEGORY;
This is the columns I want to see:
CATEGORY | JAN | FEB | MARCH | APRIL | MAY | JNE | JUL | AUG | SEPT | OCT| NOV| DEC
SENIOR 2 5 20 50 1 11 23 4 1 2 4 6
COAN 23 55 22 55 6 2 12 23 12 12 5 89
Something like this :
SELECT
SUBCATEGORY,
count( distinct case when EXTRACT(month FROM date_created) = 1 then ticket_no else null end) as JAN,
count( distinct case when EXTRACT(month FROM date_created) = 2 then ticket_no else null end) as FEB,
count( distinct case when EXTRACT(month FROM date_created) = 3 then ticket_no else null end) as MARCH,
count( distinct case when EXTRACT(month FROM date_created) = 4 then ticket_no else null end) as APRIL,
count( distinct case when EXTRACT(month FROM date_created) = 5 then ticket_no else null end) as MAY,
count( distinct case when EXTRACT(month FROM date_created) = 6 then ticket_no else null end) as JNE,
count( distinct case when EXTRACT(month FROM date_created) = 7 then ticket_no else null end) as JUL,
count( distinct case when EXTRACT(month FROM date_created) = 8 then ticket_no else null end) as AUG,
count( distinct case when EXTRACT(month FROM date_created) = 9 then ticket_no else null end) as SEPT,
count( distinct case when EXTRACT(month FROM date_created) = 10 then ticket_no else null end) as OCT,
count( distinct case when EXTRACT(month FROM date_created) = 11 then ticket_no else null end) as NOV,
count( distinct case when EXTRACT(month FROM date_created) = 12 then ticket_no else null end) as DEC
FROM
CNR_TICKET
WHERE
date_created >= to_date('1/01/2018','MM/DD/YYYY') and
date_created <= to_date('12/31/2018','MM/DD/YYYY')
GROUP BY
SUBCATEGORY
you can change your WHERE clause using :
EXTRACT(year FROM date_created ) = 2018
You may try PIVOT statement
select * from (
select SUBCATEGORY, month(date_created) mon
from CNR_TICKET
where date_created >= to_date('1/01/2018','MM/DD/YYYY') and date_created <= to_date('12/31/2018','MM/DD/YYYY')
)
pivot (
count(*)
for mon
in ( 1 Jan, 2 Feb, 3 MARCH, 4 APRIL, 5 MAY, 6 JNE, 7 JUL, 8 AUG, 9 SEPT, 10 OCT, 11 NOV, 12 DEC )
)
you can use Pivot keyword by using for month for the pivoting query as
select *
from
(
select subcategory, to_char(date_created,'mm') as month
from cnr_ticket
where to_char(date_created,'yyyy')='2018'
)
pivot(
count(*)
for (month)
in ('01' as jan ,'02' as feb, '03' as mar,
'04' as apr ,'05' as may, '06' as jun,
'07' as jul ,'08' as aug, '09' as sep,
'10' as oct ,'11' as nov, '12' as dec
)
)
or using conditional aggregation
select subcategory,
sum(case when to_char(date_created,'mm') = '01' then 1 else 0 end) as jan,
sum(case when to_char(date_created,'mm') = '02' then 1 else 0 end) as feb,
sum(case when to_char(date_created,'mm') = '03' then 1 else 0 end) as mar,
sum(case when to_char(date_created,'mm') = '04' then 1 else 0 end) as apr,
sum(case when to_char(date_created,'mm') = '05' then 1 else 0 end) as may,
sum(case when to_char(date_created,'mm') = '06' then 1 else 0 end) as jun,
sum(case when to_char(date_created,'mm') = '07' then 1 else 0 end) as jul,
sum(case when to_char(date_created,'mm') = '08' then 1 else 0 end) as aug,
sum(case when to_char(date_created,'mm') = '09' then 1 else 0 end) as sep,
sum(case when to_char(date_created,'mm') = '10' then 1 else 0 end) as oct,
sum(case when to_char(date_created,'mm') = '11' then 1 else 0 end) as nov,
sum(case when to_char(date_created,'mm') = '12' then 1 else 0 end) as dec
from cnr_ticket
where to_char(date_created,'yyyy')='2018'
group by subcategory
Rextester Demo

SQL - Calculate Customer's Percentage of Total Orders by Month

I'm practicing SQL on this site: https://www.w3schools.com/sql/trysqlserver.asp?filename=trysql_func_sqlserver_substring
, and am trying to calculate the % of total monthly orders by customer ID. So for example, if customer 10 had 3 orders in January, and there were 33 orders total in January, then customer 10's result in January would be 3/33 = 9.09%. I want each row to be a customer ID, and a column for each month.
Basically, I want to convert this:
Into this:
I can get the totals by month, but am having trouble getting the percentages.
I'm using this code:
SELECT d.CustomerID,
SUM(CASE WHEN Month = 01 THEN NumOrders ELSE 0 END) AS Jan,
SUM(CASE WHEN Month = 02 THEN NumOrders ELSE 0 END) AS Feb,
SUM(CASE WHEN Month = 03 THEN NumOrders ELSE 0 END) AS Mar,
SUM(CASE WHEN Month = 04 THEN NumOrders ELSE 0 END) AS Apr,
SUM(CASE WHEN Month = 05 THEN NumOrders ELSE 0 END) AS May,
SUM(CASE WHEN Month = 06 THEN NumOrders ELSE 0 END) AS Jun,
SUM(CASE WHEN Month = 07 THEN NumOrders ELSE 0 END) AS Jul,
SUM(CASE WHEN Month = 08 THEN NumOrders ELSE 0 END) AS Aug,
SUM(CASE WHEN Month = 09 THEN NumOrders ELSE 0 END) AS Sep,
SUM(CASE WHEN Month = 10 THEN NumOrders ELSE 0 END) AS Oct,
SUM(CASE WHEN Month = 11 THEN NumOrders ELSE 0 END) AS Nov,
SUM(CASE WHEN Month = 12 THEN NumOrders ELSE 0 END) AS [Dec],
SUM(NumOrders) AS Total
FROM(
SELECT CustomerID,
DATEPART(mm,OrderDate) AS Month,
COUNT(OrderID) AS NumOrders
FROM Orders
GROUP BY CustomerID,
DATEPART(mm,OrderDate)
) d
GROUP BY d.CustomerID
WITH ROLLUP
I've tried using this code like this to calculate the percentages, but am not getting it to work out.
SUM(CASE WHEN Month = 01 THEN NumOrders ELSE 0 END) / CAST( SUM(NumOrders) OVER (PARTITION BY Month) AS FLOAT) AS JanPct,
This is pretty basic in Excel, and seems like it should be in SQL too, so I feel like I'm missing something obvious.
Try this
Create table #tmp (CustId INT, Jan int, Feb Int, March int)
insert into #tmp VALUES
(10,4,3,5),
(11,3,1,7),
(12,6,2,6),
(13,5,4,4);
Select * from #tmp
select CustId,
CEILING(CAST(Jan As FLOAT)/CAST(SUM(Jan) OVER() AS FLOAT)*100) As Jan,
CEILING(CAST(Feb As FLOAT)/CAST(SUM(Feb) OVER() AS FLOAT)*100) As Feb,
CEILING(CAST(March As FLOAT)/CAST(SUM(March) OVER() AS FLOAT)*100) As March
from #tmp
drop table #tmp
if you want % symbol, convert to varchar and append %
Eg:
CONVERT(VARCHAR(5),CEILING(CAST(Jan As FLOAT)/CAST(SUM(Jan) OVER() AS FLOAT)*100))+'%'
I wasn't able to make rollup work with PIVOT, so here is the long solution.
DECLARE #t table(OrderId INT identity(1,1), OrderDate date, CustomerID INT)
INSERT #t values('2017-01-01', 1),('2017-01-01', 1),('2017-02-01', 1),('2017-01-01', 2)
;WITH CTE as
(
SELECT DISTINCT
CAST(ROUND(count(*) over(partition by CustomerID, Month(OrderDate))*100./ count(*)
over(partition by month(OrderDate)), 0) as INT) Pct,
Month(OrderDate) Mon,
CustomerID
FROM #t
)
SELECT
CustomerID,
SUM(CASE WHEN Mon = 1 THEN Pct ELSE 0 END) AS Jan,
SUM(CASE WHEN Mon = 2 THEN Pct ELSE 0 END) AS Feb,
SUM(CASE WHEN Mon = 3 THEN Pct ELSE 0 END) AS Mar,
SUM(CASE WHEN Mon = 4 THEN Pct ELSE 0 END) AS Apr,
SUM(CASE WHEN Mon = 5 THEN Pct ELSE 0 END) AS May,
SUM(CASE WHEN Mon = 6 THEN Pct ELSE 0 END) AS Jun,
SUM(CASE WHEN Mon = 7 THEN Pct ELSE 0 END) AS Jul,
SUM(CASE WHEN Mon = 8 THEN Pct ELSE 0 END) AS Aug,
SUM(CASE WHEN Mon = 9 THEN Pct ELSE 0 END) AS Sep,
SUM(CASE WHEN Mon = 10 THEN Pct ELSE 0 END) AS Oct,
SUM(CASE WHEN Mon = 11 THEN Pct ELSE 0 END) AS Nov,
SUM(CASE WHEN Mon = 12 THEN Pct ELSE 0 END) AS [Dec]
FROM CTE
GROUP BY ROLLUP (CustomerID)
Just Use below code instead of selecting COUNT(OrderID) AS NumOrders in your below subquery
CONVERT(numeric(10,2), count(Orderid) * 100.0/ (select count(Orderid) from [Orders])) as NumOrders

How to change column position based on condition in a select query where having with clasue

in the below query i have succeeded to get my data in a table starting from Jan, but i want to get starting month based on given date.
ex: if month(inputdate) is 11 then table columns should start from nov and end with sep
I have tried with if else and dynamic query and not succeeded because of with clause
Any help would be appreciated!
WITH taskspecdata AS (select left(datename(month,max(approvalinfo_enddate)),3)as approvedMonth,
year(max(approvalinfo_enddate)) as approvedYear, month(max(approvalInfo_endDate)) as sortMonth,
Document_Id as docid,DocVersion_Counter as versions,docversion_Id as verid
from [webcenter].[WcReports_DocApprovalQ] where
(ApprovalInfo_EndDate)>= DATEADD(year,-1,GETDATE()) and (ApprovalInfo_EndDate)<= GETDATE()
group by [WcReports_DocApprovalQ].DocVersion_Id,Document_Id,DocVersion_Counter)
select versions,
sum(case when approvedMonth = 'Jan' then 1 else 0 end) Jan,
sum(case when approvedMonth = 'Feb' then 1 else 0 end) Feb,
sum(case when approvedMonth = 'Mar' then 1 else 0 end) Mar,
sum(case when approvedMonth = 'Apr' then 1 else 0 end) Apr,
sum(case when approvedMonth = 'May' then 1 else 0 end) May,
sum(case when approvedMonth = 'Jun' then 1 else 0 end) Jun,
sum(case when approvedMonth = 'Jul' then 1 else 0 end) Jul,
sum(case when approvedMonth = 'Aug' then 1 else 0 end) Aug,
sum(case when approvedMonth = 'Sep' then 1 else 0 end) Sep,
sum(case when approvedMonth = 'Oct' then 1 else 0 end) Oct,
sum(case when approvedMonth = 'Nov' then 1 else 0 end) Nov,
sum(case when approvedMonth = 'Dec' then 1 else 0 end) Dec from taskspecdata group by versions
Result columns:
versions Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
but i want to start from input month`enter code here`
if given month is nov then
versions Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct
select id,Month where id>input data union
select id,Month where id<input data

SQL Server Pivot Row Total

I'm using SQL Server 2012 and have the below pivot code that works fine. However, how do I include a row total i.e. a sum of the recorded amount for each account over the course of the year?
SELECT *
FROM (
SELECT [Account],[AccountDesc], CONVERT(CHAR(4), AccDate, 100) as [Month], [RecordedAmount]
FROM [tblGLS215_2016_2017]
WHERE [Employee] = #Employee
) AS s
PIVOT
(
SUM ([RecordedAmount])
FOR [Month] in (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec)
) As pvt
Any help would be greatly appreciated.
If anyone is interested this is the final working solution:
SELECT pvt.* ,Isnull(pvt.jan,0) +Isnull(pvt.feb,0) +Isnull(pvt.mar,0) +Isnull(pvt.apr,0) +Isnull(pvt.may,0) +Isnull(pvt.jun,0) +Isnull(pvt.jul,0) +Isnull(pvt.aug,0) +Isnull(pvt.sept,0) +Isnull(pvt.oct,0) +Isnull(pvt.nov,0) as YearTotal
FROM (
SELECT [Account],[AccountDesc], CONVERT(CHAR(4), AccDate, 100) as [Month], [RecordedAmount]
FROM [tblGLS215_2016_2017]
WHERE [Employee] = #Employee
) AS s
pivot (
SUM ([RecordedAmount])
FOR [Month] in (May, Jun, Jul, Aug, Sept, Oct, Nov, Dec, Jan, Feb, Mar, Apr)
) As pvt
this would also work for you and might perform better
SELECT [Account],
[AccountDesc],
SUM(CASE WHEN [Month] = 'Jan' THEN [RecordedAmount] END) AS [Jan],
SUM(CASE WHEN [Month] = 'Feb' THEN [RecordedAmount] END) AS [Feb],
SUM(CASE WHEN [Month] = 'Mar' THEN [RecordedAmount] END) AS [Mar],
SUM(CASE WHEN [Month] = 'Apr' THEN [RecordedAmount] END) AS [Apr],
SUM(CASE WHEN [Month] = 'May' THEN [RecordedAmount] END) AS [May],
SUM(CASE WHEN [Month] = 'Jun' THEN [RecordedAmount] END) AS [Jun],
SUM(CASE WHEN [Month] = 'Jul' THEN [RecordedAmount] END) AS [Jul],
SUM(CASE WHEN [Month] = 'Aug' THEN [RecordedAmount] END) AS [Aug],
SUM(CASE WHEN [Month] = 'Sept' THEN [RecordedAmount] END) AS [Sept],
SUM(CASE WHEN [Month] = 'Oct' THEN [RecordedAmount] END) AS [Oct],
SUM(CASE WHEN [Month] = 'Nov' THEN [RecordedAmount] END) AS [Nov],
SUM(CASE WHEN [Month] = 'Dec' THEN [RecordedAmount] END) AS [Dec],
SUM([RecordedAmount]) AS [Total]
FROM (
SELECT [Account],
[AccountDesc],
CONVERT(CHAR(4),AccDate,100) AS [Month],
[RecordedAmount]
FROM [tblGLS215_2016_2017]
WHERE [Employee] = #Employee
) t
GROUP BY [Account],
[AccountDesc]
works the same as pivot but gives a little more control when including extra information.

SQL-Server Unpivot/Pivot Example

i'm making an application to manage hotel bookings and i need to show the ocupation rate per month in a year. I made a query that kinda solves the problem but i want presented in another format.
My current query return the following table (2x12):
January|February|March|April| ..... and so on
20 15 18 20 ..... and so on
And i want something like this (12x2):
January|20
February|15
March|18
... |...
This is my query:
Select
SUM(CASE WHEN datename(month, [CheckIn]) = 'January' or datename(month, [CheckOut]) = 'January' THEN 1 ELSE 0 END) January,
SUM(CASE WHEN datename(month, [CheckIn]) = 'February' or datename(month, [CheckOut]) = 'February' THEN 1 ELSE 0 END) February,
SUM(CASE WHEN datename(month, [CheckIn]) = 'March' or datename(month, [CheckOut]) = 'March' THEN 1 ELSE 0 END) March,
SUM(CASE WHEN datename(month, [CheckIn]) = 'April' or datename(month, [CheckOut]) = 'April' THEN 1 ELSE 0 END) April,
SUM(CASE WHEN datename(month, [CheckIn]) = 'May' or datename(month, [CheckOut]) = 'May' THEN 1 ELSE 0 END) May,
SUM(CASE WHEN datename(month, [CheckIn]) = 'June' or datename(month, [CheckOut]) = 'June' THEN 1 ELSE 0 END) June,
SUM(CASE WHEN datename(month, [CheckIn]) = 'July' or datename(month, [CheckOut]) = 'July' THEN 1 ELSE 0 END) July,
SUM(CASE WHEN datename(month, [CheckIn]) = 'August' or datename(month, [CheckOut]) = 'August' THEN 1 ELSE 0 END) August,
SUM(CASE WHEN datename(month, [CheckIn]) = 'September' or datename(month, [CheckOut]) = 'September' THEN 1 ELSE 0 END) September,
SUM(CASE WHEN datename(month, [CheckIn]) = 'October' or datename(month, [CheckOut]) = 'October' THEN 1 ELSE 0 END) October,
SUM(CASE WHEN datename(month, [CheckIn]) = 'November' or datename(month, [CheckOut]) = 'November' THEN 1 ELSE 0 END) November,
SUM(CASE WHEN datename(month, [CheckIn]) = 'December' or datename(month, [CheckOut]) = 'December' THEN 1 ELSE 0 END) December
FROM {Booking} INNER JOIN {Status} ON {Booking}.[StatusId] = {Status}.[Id]
WHERE {Booking}.[CheckIn] >= #BeginDate AND {Booking}.[CheckOut] <= #EndDate AND {Status}.[Label] <> 'Canceled'
Any help would be appreciated, i'm stuck and theres is not that much info on the web, thanks!
Looks like you're trying to do an UNPIVOT:
SELECT Month, CheckIns
FROM
(Select
SUM(CASE WHEN datename(month, [CheckIn]) = 'January' or datename(month, [CheckOut]) = 'January' THEN 1 ELSE 0 END) January,
SUM(CASE WHEN datename(month, [CheckIn]) = 'February' or datename(month, [CheckOut]) = 'February' THEN 1 ELSE 0 END) February,
SUM(CASE WHEN datename(month, [CheckIn]) = 'March' or datename(month, [CheckOut]) = 'March' THEN 1 ELSE 0 END) March,
SUM(CASE WHEN datename(month, [CheckIn]) = 'April' or datename(month, [CheckOut]) = 'April' THEN 1 ELSE 0 END) April,
SUM(CASE WHEN datename(month, [CheckIn]) = 'May' or datename(month, [CheckOut]) = 'May' THEN 1 ELSE 0 END) May,
SUM(CASE WHEN datename(month, [CheckIn]) = 'June' or datename(month, [CheckOut]) = 'June' THEN 1 ELSE 0 END) June,
SUM(CASE WHEN datename(month, [CheckIn]) = 'July' or datename(month, [CheckOut]) = 'July' THEN 1 ELSE 0 END) July,
SUM(CASE WHEN datename(month, [CheckIn]) = 'August' or datename(month, [CheckOut]) = 'August' THEN 1 ELSE 0 END) August,
SUM(CASE WHEN datename(month, [CheckIn]) = 'September' or datename(month, [CheckOut]) = 'September' THEN 1 ELSE 0 END) September,
SUM(CASE WHEN datename(month, [CheckIn]) = 'October' or datename(month, [CheckOut]) = 'October' THEN 1 ELSE 0 END) October,
SUM(CASE WHEN datename(month, [CheckIn]) = 'November' or datename(month, [CheckOut]) = 'November' THEN 1 ELSE 0 END) November,
SUM(CASE WHEN datename(month, [CheckIn]) = 'December' or datename(month, [CheckOut]) = 'December' THEN 1 ELSE 0 END) December
FROM {Booking} INNER JOIN {Status} ON {Booking}.[StatusId] = {Status}.[Id]
WHERE {Booking}.[CheckIn] >= #BeginDate AND {Booking}.[CheckOut] <= #EndDate AND {Status}.[Label] <> 'Canceled'
) monthTotals
UNPIVOT
(CheckIns FOR Month IN
(January, February, March, April, May, June, July, August, September, October, November, December)
) AS upvt
Here's a simplified SQLFiddle of this
And here's a doc page on PIVOT and UNPIVOT
You can make your case statements a little shorter/simpler so it's easier to read. MONTH() is a easier to read function and getting rid of else will mean if it simply return a null which SUM() treats like a 0
SELECT [Month], CheckIns
FROM
(
Select
SUM(CASE WHEN MONTH([CheckIn]) = 01 or MONTH([CheckOut]) = 01 THEN 1 END) January,
SUM(CASE WHEN MONTH([CheckIn]) = 02 or MONTH([CheckOut]) = 02 THEN 1 END) February,
SUM(CASE WHEN MONTH([CheckIn]) = 03 or MONTH([CheckOut]) = 03 THEN 1 END) March,
SUM(CASE WHEN MONTH([CheckIn]) = 04 or MONTH([CheckOut]) = 04 THEN 1 END) April,
SUM(CASE WHEN MONTH([CheckIn]) = 05 or MONTH([CheckOut]) = 05 THEN 1 END) May,
SUM(CASE WHEN MONTH([CheckIn]) = 06 or MONTH([CheckOut]) = 06 THEN 1 END) June,
SUM(CASE WHEN MONTH([CheckIn]) = 07 or MONTH([CheckOut]) = 07 THEN 1 END) July,
SUM(CASE WHEN MONTH([CheckIn]) = 08 or MONTH([CheckOut]) = 08 THEN 1 END) August,
SUM(CASE WHEN MONTH([CheckIn]) = 09 or MONTH([CheckOut]) = 09 THEN 1 END) September,
SUM(CASE WHEN MONTH([CheckIn]) = 10 or MONTH([CheckOut]) = 10 THEN 1 END) October,
SUM(CASE WHEN MONTH([CheckIn]) = 11 or MONTH([CheckOut]) = 11 THEN 1 END) November,
SUM(CASE WHEN MONTH([CheckIn]) = 12 or MONTH([CheckOut]) = 12 THEN 1 END) December
FROM [Booking]
INNER JOIN {Status} ON {Booking}.[StatusId] = {Status}.[Id]
WHERE {Booking}.[CheckIn] >= #BeginDate AND {Booking}.[CheckOut] <= #EndDate AND {Status}.[Label] <> 'Canceled'
) MONTHTotals
UNPIVOT
(
CheckIns FOR [Month] IN
(January, February, March, April, May, June, July, August, September, October, November, December)
) AS upvt