I am looking for help generating a table with dynamic columns and calculated values in SQL Server from the following (abbreviated) source tables:
Assignments Source Table
Name StartDate EndDate Value Factor Jim 2013-08-01 2013-09-06 200.0 0.5 Bob 2013-07-27 2013-11-01 140.0 1.0 Alice 2013-08-29 2014-03-22 200.0 0.8 Jim 2013-08-20 2013-09-01 250.0 0.5
Reporting Weeks Source Table
WeekEndDate 2013-08-18 2013-08-25 2013-09-01 2013-09-08
Desired Results Table
Name StartDate EndDate Value Factor 2013-08-18 2013-08-25 2013-09-01 2013-09-08 Jim 2013-08-01 2013-09-06 200.0 0.5 100.0 100.0 100.0 0.0 Bob 2013-07-27 2013-11-01 140.0 1.0 140.0 140.0 140.0 140.0 Alice 2013-08-29 2014-03-22 200.0 0.8 0.0 0.0 160.0 160.0 Jim 2013-08-20 2013-09-01 250.0 0.5 0.0 125.0 125.0 0.0
Basically, I need to turn the reporting rows table into columns and then calculate a value where the pivoted column date (Reporting Weeks/WeekEndDate) is between the StartDate and EndDate. If the date is outside that range the value should be set to zero. The "Reporting Weeks" table can vary over time, and may be generated from another query. The StartDate and EndDate will usually not match the WeekEndDate. I have been going over a lot of questions/answers around dynamic sql pivots, but haven't found anything that I can adapt to fit the need given my limited background in this area.
Before tackling the dynamic SQL version of this, I would first write the query as a static version using your limited dates. In order to get the result, I would calculate the value that you need in a subquery and then pivot on the
weekenddates
.The basic syntax if you have a limited number of values would be:
See SQL Fiddle with Demo. Once you have the correct logic, then you can convert the query into dynamic SQL:
See SQL Fiddle with Demo. Both will give a result: