SQL Matrix to array -
i working t-sql , have table looks matrix (8x8). objective make matrix (table) array using pivot ... have read forums , more stuff managed find still can't make code ...
id bucket b1 b2 b3 b4 5 1 20 21 45 12 6 2 12 18 19 48 7 3 19 78 40 78 8 4 72 34 12 17
so need make "three dimensional array" table, , save row, column , value ... this
row column value 1 1 20 1 2 21 1 3 45 1 2 12 etc etc etc 4 3 12 4 4 17
does have idea how write code in t-sql?
ps. reason i'm doing this, because want multiply matrix itself. it's easier multiply if have in pivot table. thank you
try unpivoting data :
declare @table table (id int, bucket int, b1 int, b2 int, b3 int, b4 int) insert @table values (5,1,20,21,45,12), (6,2,12,18,19,48), (7,3,19,78,40,78), (8,4,72,34,12,17) select rn [row], value [columnnumber], orders [value] ( select row_number () on (order id) rn,id, bucket, b1 [1], b2 [2], b3 [3], b4 [4] @table ) t unpivot ( orders value in([1], [2],[3],[4]) ) pvt
check msdn doc more details of pivot , unpivot.
Comments
Post a Comment