I have developed Financial Year Data Fiscal year wise Till now i have achieved! But i couldn't get column-wise average
My table Definition
CREATE TABLE [dbo].[tblact] (
[Id] INT NOT NULL,
[years] NVARCHAR (MAX) NULL,
[months] NVARCHAR (MAX) NULL,
[expenses] DECIMAL (18, 2) NULL,
[closingbal] DECIMAL (18, 2) NULL,
[monthorder] INT NULL
My Query
CREATE PROCEDURE fiscalyear
AS
DECLARE @qstr AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
declare @sql nvarchar(max)
SELECT @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(years)FROM (SELECT DISTINCT years FROM tblact) AS years;
SET @qstr ='SELECT months, ' + @ColumnName + ',total,average FROM
(SELECT months, years, expenses,avg(expenses) over(partition by months) average,sum(expenses) over (partition by months) total ,monthorder FROM tblact ) AS p
PIVOT(SUM(expenses) FOR years IN (' + @ColumnName + ')) AS PVTTable order by monthorder ';
EXEC sp_executesql @qstr
Kindly please do help to acheive the answer
My output Now :
Months | 2009-2010 | 2010 - 2011 | 2012-2013 | Total | Average
--------------------------------------------------------------
April | 2000 | 3000 | 4000 | 9000 | 3000
MAY | 2000 | 3000 | 4000 | 9000 | 3000
--------------------------------------------------------------
Expected Output
Months | 2009-2010 | 2010 - 2011 | 2012-2013 | Total | Average
--------------------------------------------------------------
April | 2000 | 3000 | 4000 | 9000 | 3000
MAY | 2000 | 3000 | 4000 | 9000 | 3000
--------------------------------------------------------------
Average| 2000 | 3000 | 4000 | 9000 | 3000
Kindly Help !
I cleaned up your code a bit. I simply use a UNION ALL and AVG your columns. Note: I use 13 as the month order for the 'Average' row so when it's ordered, it goes at the end. If you need anything else let me know.