Skip to content
Advertisement

SQL – Calculating third column from previous two

I have just started SQL and have some difficulties in terms of thinking in this language.

I have now a task where I need to create a new column in a table with values from previous columns. This calculation is based on few conditions. Is this even possible to do easily in SQL?

Let’s say I have a table called dbo.country_sales where

generates

I need to create fourth column with sales development between 2015 and 2017 (sales in 2017 – sales in 2015) so the table would look like:

I know how to make it in the select statement but it calculates only 1 value. Tried to search for some hours but did not find anything simple.

Thanks for help. Lan

Advertisement

Answer

You can use CTEs to precompute values. For example:

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