Hi Everyone,
How do I calculate a Percentage difference between months in SQL
In my SQL I have a DataTable with records which I am trying to create a SQL script that shows me the difference percentage between months.
So the difference from previous sum of prices to the current month sum of prices. But I just want to insert the current month and based on current month the system to take the last month automatic.
SELECT
Sum(Price) As Total,
CONVERT(char(7), date, 120) As [year],
Department ,
Sum Dinstict (CONVERT(INT, CONVERT(VARCHAR(MAX),quantity))) as Lesh
FROM
dbo.Expenses
WHERE
CONVERT(char(7), date, 120) ='2020-01' AND
Department = 'FixCost'
GROUP BY
CONVERT(char(7), date, 120),
Department
)
SELECT
Total,
[year],
Department as 'Department',
Concat(SUM(Convert(int,Convert(Varchar(MAX),(Lesh)))) OVER (),'') As GrandTotal
FROM
cte
WHERE
[year] Is Not Null
Best Regards,
Elvis