sql server 2008 r2 - Multi-Column, Multi-Row PIVOT -


consider have table contains data in following form:

foo_fk  monthcode_fk  activity_fk  sumresultsx  sumresultsy ----------------------------------------------------------- 1       201312        0            10           2 1       201312        1            5            1 1       201401        0            15           3 1       201401        1            7            2 2       201312        0            9            3 2       201312        1            1            2 2       201401        0            6            2 2       201401        1            17           4 

for purposes, safe assume table aggregation have been created group by on foo_fk, monthcode_fk, activity_fk sum( resultsa ), sum( resultsb ) obtain data, making foo_fk, monthcode_fk, activity_fk unique per record.

if reason found preferable pivot table in stored procedure ease amount of screwing around ssrs i'd have ( , undoubtedly later maintain ), wishing following format consumption via matrix tablix thingy:

foo_fk  1312_0_x  1312_0_y  1312_1_x  1312_1_y  1401_0_x  1401_0_y  1401_1_x  1401_1_y   -------------------------------------------------------------------------------------- 1       10        2         5         1         15        3         7         2 2       9         3         1         2         6         2         17        4 

how go doing in not-mental way? please refer sql fiddle @ proof trying use hammer build device pushes in nails. don't worry dynamic version i'm sure can figure out once i'm guided through static solution test case.

right now, i've tried create foo_fk, monthcode_fk set via following, attempt pivot ( see fiddle full mess ):

select  foo_fk = isnull( a0.foo_fk, a1.foo_fk ),         monthcode_fk = isnull( a0.monthcode_fk, a1.monthcode_fk ),         [0_x] = isnull( a0.sumresultx, 0 ),         [0_y] = isnull( a0.sumresulty, 0 ),         [1_x] = isnull( a1.sumresultx, 0 ),         [1_y] = isnull( a1.sumresulty, 0 ) (  select  foo_fk, monthcode_fk, activity_fk,                  sumresultx, sumresulty            dbo.t_fooactivitybymonth           activity_fk = 0 ) a0 full outer join (         select  foo_fk, monthcode_fk, activity_fk,                  sumresultx, sumresulty            dbo.t_fooactivitybymonth           activity_fk = 1 ) a1     on  a0.foo_fk = a1.foo_fk; 

i have come across excellent advice on question, i'm in process of performing form of unpivot before twist out using pivot , max, if there's better way this, i'm ears.

it seems should able applying unpivot sumresultx , sumresulty columns first, pivoting data:

;with cte (   select foo_fk,      col = cast(monthcode_fk varchar(6))+'_'             +cast(activity_fk varchar(1))+'_'+sumresult,      value   dbo.t_fooactivitybymonth   cross apply   (     values        ('x', sumresultx),       ('y', sumresulty)   ) c (sumresult, value) )  select foo_fk,    [201312_0_x], [201312_0_y], [201312_1_x], [201312_1_y],   [201401_0_x], [201401_0_y], [201401_1_x], [201401_1_y] cte pivot (   max(value)   col in ([201312_0_x], [201312_0_y], [201312_1_x], [201312_1_y],               [201401_0_x], [201401_0_y], [201401_1_x], [201401_1_y]) ) piv; 

see sql fiddle demo


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -