I believe I will have to use both a Pivot and a UnPivot to get the data that I want.
My current data looks like this:
var_date var_attribute var_out_of_ad var_per_ad
9/4/2012 Aux1 0 0
9/5/2012 Aux1 0 0
9/4/2012 Aux2 1680 0.733333333
9/5/2012 Aux2 1680 0.733333333
9/4/2012 Aux3 1500 0.791666667
9/5/2012 Aux3 1500 0.791666667
9/4/2012 Aux4 0 0
9/5/2012 Aux4 0 0
9/4/2012 Aux5 0 0
9/5/2012 Aux5 0 0
9/4/2012 Aux6 26640 0.766929134
9/5/2012 Aux6 26640 0.766929134
9/4/2012 Aux7 28800 0
9/5/2012 Aux7 28800 0
9/4/2012 Aux8 3600 0
9/5/2012 Aux8 3600 0
I want to be able to break apart by Aux Code (which will by dynamic) the columns var_out_of_ad and var_per_ad so I would like my data to look like this:
var_date Aux1_per Aux1_out Aux2_per Aux2_out ...
9/4/2012 0 0 .733 1680
9/5/2012 0 0 .733 1680
I have put the testing data into a sql fiddle: http://sqlfiddle.com/#!3/7f06d/1 as well as all of the work I have done with the dynamic pivot.
Can someone please guide me as to what to do next? I am lost.
Thank You
Since you are trying to
PIVOTtwo columns of data, you need toUNPIVOTfirst, then apply aPIVOT:See SQL Fiddle with Demo