Skip to content
Advertisement

How to calculate the difference in revenue for 2 specific periods from data in a single table?

I am using SQL Server 2014 and I have the following T-SQL query which is supposed to calculate the difference in total revenue based on 2 specific periods. The data to perform the computation come from a single SQL Table.

To summarize, I have a column in table T1 called Revenue and another column called Month. I need to find the difference in revenue for Months September 2020 to December 2020 versus September 2019 to December 2019.

My T-SQL query stands as follows:

I have based my query on the following: How to calculate between different group of rows of the same table

However, when running my query, I am getting the following error message:

Invalid column name ‘ID’.

I can’t figure out what I am doing wrong here. Isn’t column ID present in the cte3?

Advertisement

Answer

ID must be present in the select list of both a and b for it to be visible to the join:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement