SQL Server Pivot Row Total - sql

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.

Related

Is it possible to build this table using SQL?

I have 2 tables, a Sales table and a Payment table structured like the below.
The 2 are joined using the ContractID column. What I want to see is a matrix that shows me at the top, the sum of (sold amount) per monthyear. Then on the left, I want to see the payment dates by month year, and any payments that have been made. My ideal output would look like the below.
The yellow line being the total sold by month-year, and the green lines being all the payments that have been made from the payments table. I don't really know where to start with this one, does anyone have any advice on how to achieve this? I am going to unpivot the sold table first to get my dates across the top, just pondering the next step to pull this table together?
If I didn't understand wrong, it should be like this.
WITH PaymentMatrix
AS
(
SELECT
PaymentMonth,
SoldAmount,
[1] AS Jan,
[2] AS Feb,
[3] AS Mrz,
[4] AS Apr,
[5] AS Mai,
[6] AS Jun,
[7] AS Jul,
[8] AS Aug,
[9] AS Sep,
[10] AS Okt,
[11] AS Nov,
[12] AS Dez
FROM
(
Select
MONTH(S.SoldDate) as SoldMonth,
MONTH(P.PaymentDate) as PaymentMonth,
SUM(S.SoldAmount) as SoldAmount,
SUM(P.PaymentAmount) as PaymentAmount
from Sales S
INNER JOIN Payment P ON S.ContractID = P.ContractID
GROUP BY
MONTH(S.SoldDate),
MONTH(P.PaymentDate)
) source
PIVOT
(
SUM(PaymentAmount)
FOR SoldMonth
IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
) AS pvtMonth
)
SELECT
PaymentMonth,
SUM(SoldAmount) AS Sold,
sum(Jan)as Jan , sum(Feb) as Feb, sum(Mrz) as Mrz, sum(Apr) as Apr, sum(Mai) as Mai,
sum(Jun)as Jun , sum(Jul) as Jul, sum(Aug) as Aug, sum(Sep) as Sep, sum(Okt) as Okt,
sum(Nov) as Nov, sum(Dez) as Dez
FROM PaymentMatrix
GROUP BY PaymentMonth
Fidler Sample
Sample Image
I suggest using conditional aggregation and a union.
Since the PIVOT syntax is more limited.
SELECT [Sold], [Jan-22], [Feb-22], [Mar-22]
FROM
(
SELECT 0 as Seq, 'Paid' AS [Sold]
, SUM(CASE WHEN FORMAT([Sold Date],'MMM-yy') = 'Jan-22'
THEN [Sold Amount] ELSE 0 END) AS [Jan-22]
, SUM(CASE WHEN FORMAT([Sold Date],'MMM-yy') = 'Feb-22'
THEN [Sold Amount] ELSE 0 END) AS [Feb-22]
, SUM(CASE WHEN FORMAT([Sold Date],'MMM-yy') = 'Mar-22'
THEN [Sold Amount] ELSE 0 END) AS [Mar-22]
FROM Sales
UNION ALL
SELECT m.Seq, m.PaymentMonth
, SUM(CASE WHEN SoldMonth = 'Jan-22' THEN PaymentAmount ELSE 0 END) AS [Jan-22]
, SUM(CASE WHEN SoldMonth = 'Feb-22' THEN PaymentAmount ELSE 0 END) AS [Feb-22]
, SUM(CASE WHEN SoldMonth = 'Mar-22' THEN PaymentAmount ELSE 0 END) AS [Mar-22]
FROM (VALUES
(1,'Jan-22'),
(2,'Feb-22'),
(3,'Mar-22')
) m(Seq, PaymentMonth)
LEFT JOIN (
SELECT ContractID
, FORMAT(EOMONTH([Payment Date]), 'MMM-yy') AS PaymentMonth
, SUM([Payment Amount]) AS PaymentAmount
FROM Payment
GROUP BY ContractID, EOMONTH([Payment Date])
) p ON p.PaymentMonth = m.PaymentMonth
LEFT JOIN (
SELECT ContractID
, FORMAT(MAX([Sold Date]), 'MMM-yy') AS SoldMonth
, SUM([Sold Amount]) AS SoldAmount
FROM Sales
GROUP BY ContractID
) s ON s.ContractID = p.ContractID
GROUP BY m.Seq, m.PaymentMonth
) q
ORDER BY Seq;
Sold
Jan-22
Feb-22
Mar-22
Paid
2500
100
0
Jan-22
300
0
0
Feb-22
400
50
0
Mar-22
0
0
0
Test on db<>fiddle here

SQL Query for count by month over multiple years

I've been using the query below to get me a count of records by month. Works good for a specific year.
However, its a bit of a pain if I want to compare a count from Jan 2018 to Jan 2019. Especially if I want to graph it.
How might I adjust this so that I could get a Jan 18, Feb 18,....Jan 19 type of result by giving a span of years.
This is Postgres 9.6
Thanks!
SELECT
count(case when to_char(t.order_date, 'MM') = '01' then 1 END) as Jan,
count(case when to_char(t.order_date, 'MM') = '02' then 1 END) as Feb,
count(case when to_char(t.order_date, 'MM') = '03' then 1 END) as Mar,
count(case when to_char(t.order_date, 'MM') = '04' then 1 END) as Apr,
count(case when to_char(t.order_date, 'MM') = '05' then 1 END) as May,
count(case when to_char(t.order_date, 'MM') = '06' then 1 END) as June,
count(case when to_char(t.order_date, 'MM') = '07' then 1 END) as Jul,
count(case when to_char(t.order_date, 'MM') = '08' then 1 END) as Aug,
count(case when to_char(t.order_date, 'MM') = '09' then 1 END) as Sep,
count(case when to_char(t.order_date, 'MM') = '10' then 1 END) as Oct,
count(case when to_char(t.order_date, 'MM') = '11' then 1 END) as Nov,
count(case when to_char(t.order_date, 'MM') = '12' then 1 END) as Dec
FROM transactions as t
WHERE to_char(t.order_date, 'YYYY') = '2019'
You can make “buckets” of your data by timestamp, by combining:
Truncate a timestamp value to a specified precision, flattening those values to a single value.
Group by the resulting lower-precision values.
SELECT
date_trunc('month', t.order_date) AS order_month,
count(t.order_id) AS count
FROM transaction AS t
GROUP BY order_month
ORDER BY order_month
Then it's up to you what years to limit the result to:
SELECT
date_trunc('month', t.order_date) AS order_month,
count(t.order_id) AS count
FROM transaction AS t
WHERE
date_part('year', t.order_date) = 2019
GROUP BY order_month
ORDER BY order_month
Please check below query, is this relevant?
Blockquote
SELECT
to_char(order_date, 'YYYY'),
count(case when to_char(t.order_date, 'MM') = '01' then 1 END) as Jan,
count(case when to_char(t.order_date, 'MM') = '02' then 1 END) as Feb,
count(case when to_char(t.order_date, 'MM') = '03' then 1 END) as Mar,
count(case when to_char(t.order_date, 'MM') = '04' then 1 END) as Apr,
count(case when to_char(t.order_date, 'MM') = '05' then 1 END) as May,
count(case when to_char(t.order_date, 'MM') = '06' then 1 END) as June,
count(case when to_char(t.order_date, 'MM') = '07' then 1 END) as Jul,
count(case when to_char(t.order_date, 'MM') = '08' then 1 END) as Aug,
count(case when to_char(t.order_date, 'MM') = '09' then 1 END) as Sep,
count(case when to_char(t.order_date, 'MM') = '10' then 1 END) as Oct,
count(case when to_char(t.order_date, 'MM') = '11' then 1 END) as Nov,
count(case when to_char(t.order_date, 'MM') = '12' then 1 END) as Dec
FROM transactions as t where to_char(order_date, 'YYYY') in ('2018','2019')
group by to_char(order_date, 'YYYY');
Blockquote
Do you want the results on different rows?
SELECT to_char(t.order_date, 'YYYY') = '2019',
count(*) filter (to_char(t.order_date, 'MM') = '01') as Jan,
count(*) filter (to_char(t.order_date, 'MM') = '02') as Feb,
. . .
count(*) filter (to_char(t.order_date, 'MM') = '12') as Dec
FROM transactions as t
GROUP BY to_char(t.order_date, 'YYYY') = '2019'

How to insert a TOTAL column in this SQL Pivot query?

I have a SQL Pivot query which works fine. However, I need to add a TOTAL column in the output (which will be the SUM of all the months).
My query (extract) stands as follows:
...
SELECT [Year],[Property], ISNULL([Jan],0) as [Jan], ISNULL([Feb],0) as [Feb], ISNULL([Mar],0) as [Mar], ISNULL([Apr],0) as [Apr], ISNULL([May],0) as [May], ISNULL([Jun],0) as [Jun],
ISNULL([Jul],0) as [Jul], ISNULL([Aug],0) as [Aug], ISNULL([Sep],0) as [Sep], ISNULL([Oct],0) as [Oct], ISNULL([Nov],0) as [Nov], ISNULL([Dec],0) as [Dec]
FROM (
SELECT [MthName], [Year], [RN], [Property] from CTE1
UNION ALL
SELECT [MthName], [Year], [RN], [Property] from CTE2
)x
PIVOT(SUM(x.[RN])
FOR x.[MthName] IN ([Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec])) AS PVTTable
ORDER BY [Property], [Year] DESC
An extract of the output of this query is shown below:
Year Property Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2018 XYZ 3261 2955 3045 2118 1917 869 1772 1598 834 1254 537 415
2017 XYZ ...
I need to add a 'Total' column at the end of this output. How can I do this?
Add this part at the last in your Select query:
(ISNULL([Jan],0)+ ISNULL([Feb],0) + ISNULL([Mar],0)+ ISNULL([Apr],0)+ ISNULL([May],0)+ ISNULL([Jun],0) + ISNULL([Jul],0)+ ISNULL([Aug],0)+ ISNULL([Sep],0)+ ISNULL([Oct],0) + ISNULL([Nov],0)+ ISNULL([Dec],0)) as [Total]
I would use conditional aggregation approach instead of using cte with PIVOT operation
select Year,
Property,
SUM(case when MthName = 'Jan' then RN else 0 end) Jan,
SUM(case when MthName = 'Feb' then RN else 0 end) Feb,
SUM(case when MthName = 'Mar' then RN else 0 end) Mar,
...
SUM(RN) Total
from table t
group by year, Property

SQL Pivot Table from query

How can I convert this query to pivot
select
branch,
months,
[Parts Revenue Budget]
from #Temp1
Result
Desired Output
You can use conditional aggregation :
SELECT t.branch,
SUM(CASE WHEN t.months = 'JAN' THEN t.[Parts Revenue Budget] ELSE 0 END) as JAN,
SUM(CASE WHEN t.months = 'FEB,' THEN t.[Parts Revenue Budget] ELSE 0 END) as FEB
... As many as you need
SUM(t.[Parts Revenue Budget]) as grandtotal
FROM #Temp1
GROUP BY t.branch
UNION ALL
SELECT 'Grand Total',
SUM(CASE WHEN t.months = 'JAN' THEN t.[Parts Revenue Budget] ELSE 0 END) as JAN,
SUM(CASE WHEN t.months = 'FEB' THEN t.[Parts Revenue Budget] ELSE 0 END) as FEB,
... As many as you need
SUM(t.[Parts Revenue Budget]) as grandtotal
FROM #Temp1 t
SELECT BRANCH,
SUM(CASE WHEN MONTHS = 'JAN' THEN PARTSREVENUEBUDJET END) AS [JAN],
SUM(CASE WHEN MONTHS = 'FEB' THEN PARTSREVENUEBUDJET END) AS [FEB],
SUM(CASE WHEN MONTHS = 'MAR' THEN PARTSREVENUEBUDJET END) AS [MARCH],
SUM(CASE WHEN MONTHS = 'APR' THEN PARTSREVENUEBUDJET END) AS [APR],
SUM(CASE WHEN PONITS = 'MAY' THEN PARTSREVENUEBUDJET END) AS [MAY],
SUM(CASE WHEN MONTHS = 'JUNE' THEN PARTSREVENUEBUDJET END) AS [JUNE],
SUM(CASE WHEN MONTHS = 'JULY' THEN PARTSREVENUEBUDJET END) AS [JULY],
SUM(CASE WHEN MONTHS = 'AUG' THEN PARTSREVENUEBUDJET END) AS [AUG],
SUM(CASE WHEN MONTHS = 'SEP' THEN PARTSREVENUEBUDJET END) AS [SEP],
SUM(CASE WHEN MONTHS = 'OCT' THEN PARTSREVENUEBUDJET END) AS [OCT],
SUM(CASE WHEN MONTHS = 'NOV' THEN PARTSREVENUEBUDJET END) AS [NOV],
SUM(CASE WHEN MONTHS = 'NOV' THEN PARTSREVENUEBUDJET END) AS [DEC],
SUM(PARTSREVENUEBUDJET) AS 'GRANDTOTAL'
FROM TABLE
GROUP BY BRANCH
2)
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(months)
from #TEMP1
group by months
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT branch,' + #cols + ',tiot from
(
select *,sum(BUDGET)over(partition by branch) as tiot
from #TEMP1
) x
pivot
(
sum(BUDGET)
for months in (' + #cols + ')
) p '
exec(#query);
I tried as follows:
CREATE TABLE #TEMP1(BRANCH VARCHAR(100),MONTHS VARCHAR(100),BUDGET varchar(100))
INSERT INTO #TEMP1 VALUES ('CPD','APR','801375')
INSERT INTO #TEMP1 VALUES ('Sabzi Mandi','APR','1033697')
INSERT INTO #TEMP1 VALUES ('DHA','APR','2119835')
INSERT INTO #TEMP1 VALUES ('Quidabad','APR','2100042')
INSERT INTO #TEMP1 VALUES ('Sukkar','AUG','44377320')
INSERT INTO #TEMP1 VALUES ('Baghbanpura','AUG','2726114')
INSERT INTO #TEMP1 VALUES ('Multan','AUG','6068565')
select *,coalesce(CAST(AUG AS NUMERIC(10,2)),0)+coalesce(CAST(apr AS NUMERIC(10,2)),0) as 'grand total'
from
(select branch , months ,budget from #temp1) as s
pivot
(MAX(BUDGET) for months in (APR,AUG)) AS PVT
CREATE TABLE TEMP1(BRANCH VARCHAR(100),MONTHS VARCHAR(100),BUDGET FLOAT(126));
INSERT INTO TEMP1 VALUES ('ABC','AUG','1000');
INSERT INTO TEMP1 VALUES ('PQR','APR','2000');
INSERT INTO TEMP1 VALUES ('XYZ','AUG','1000');
SELECT
BRANCH,
APR,
AUG,
(NVL(APR,'0')+NVL(AUG,'0')) AS GRANDTOTAL
FROM
(SELECT
*
FROM
(
SELECT
BRANCH,
MONTHS,
BUDGET
FROM
TEMP1
)
pivot ( MIN(BUDGET) FOR MONTHS IN ('APR' AS APR,'AUG' AS AUG)))
UNION ALL
SELECT 'GRAND TOTAL' AS BRANCH,SUM(APR) AS APR, SUM(AUG) AS AUG, SUM(GRANDTOTAL) AS GRANDTOTAL
FROM
(SELECT
BRANCH,
APR,
AUG,
(NVL(APR,'0')+NVL(AUG,'0')) AS GRANDTOTAL
FROM
(SELECT
*
FROM
(
SELECT
BRANCH,
MONTHS,
BUDGET
FROM
TEMP1
)
pivot ( MIN(BUDGET) FOR MONTHS IN ('APR' AS APR,'AUG' AS AUG) )));

Query optimization?

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]