Skip to content
Advertisement

SQL Server – Calculating target vs actual values per week for sales overview

I am new to SQL. I am trying to create an over view using these 2 tables

Actual sale

Week_Year

Target sale

To combine into this resulting view with Targets and Actuals:

where column Target is ‘Total to be sold’ spread linearly between the available weeks.

How can I achieve this using SQL Server? Any inputs much appreciated. Thanks.

Advertisement

Answer

In order to make the week numbers (34, 35, 36, 37) correspond to system weeks the variable @start_wk_no sets the starting point. The actual sales needs to be unpivoted to join with projected sales. The query uses a tally (or numbers) function to generate the rows.

Data

Query

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