sql server group columns as rows (pivot?) -


I have found this result data in SQL Server 2008 R2

  ID size Sum of Avg1 Avg2 A1, A2 A3 1 3921 39 690 17.69 0.18 Zero zero zero 40 11979 301 5944,26 19,75 0,5 10000,00 2000,00 1000,00 41 11714 289 5060 17,51 0,43 10000, 00 3000,00 2000,00 42 11599 265 4107,98 15,5 0,35 10000,00 5000,00 500,00   

And to move columns in rows according to ID So I want to get this result:

  ID1 id40 id41 id42 1 40 41 42 3921 11979 11714 11599 39 301 289 265 690 5944 5060 4107 17,69 19,75 17,51 15 , 5 0,18 0,5 0,43 0,35 10000,00 2000,00 1000,00 1000000 3000,00 2000,00 10000,00 5000,00 500,00   

Is there any way to do this? I tried the pivot, but as far as I tried that I can only change 1 column and in this case I am not required.

To achieve this result, you want to first unpivot the data of the rows in the column, and then apply the Pivot function.

Since you are using SQL Server 2008, you can use CROSS APPLY and VALUES to unpout the data. It converts from many of your columns and converts them into rows:

  select 'id' + cast (TV as varchar (10)) p_id, c.col, C.value, (4, 'sum', amount), (3, 'acted', action), (3, 'action', size), (2, 'size', size) (value (1, 'id ',' 8 ',' A1 ', a1), (8,' A2 ',' A2 '), (9,' ',' ',' ',' 5 ',' avg1 ', avg1), (6,' avg2 ', avg2) A3 ', A3)) See C (sort_order, col, value)   

Once the data has been unpigmented, you can pivot using the new column, which is the id value, then the full code is:

  select col, id1 , Id40, id41, id42 (select 'id' + cast (TV as vidar (10)) Apply p_id, c.col, series, from your satellite T (value (1, 'id', id ), (2, 'shape', size), (3, 'acted', acted), (4, 'yoga' (9, 'a3'), (5, 'ag 1', ag 1) 6, 'AG2', AG2), (7, 'A1', A1), (8, 'A2', A2), A3)) c (sort_order, col, value) p_id in p_id (Max1), (id1, id40, id41, id42)) see piv order sort_order (max)   

Not.

If you can not use cross-apali and VALUES , then it also did using the UNPIVOT function Can be: Select 'ID', then select '1' ('id' + cast (id_piv varchar (10)) id, col, value, case col, to col, id1, select id value column, see the piv command; sort_order

Then you as NVARCHAR (Max), @query as

  R (Max) Select @cols declaration @cols = STUFF (Select the ',' + 'QUOTENAME' ('id': Dynamic SQL to be used (POTH '', type) by ID for its object group From ++ (ID as cast (varchar (10)). Price ('.', 'NVARCHAR (MAX)'), 1,1,) Settable @query = 'SELECT col,' + cols + '(Select "ID" + Cast (TCT varchar (10)) P_id, c.col, c.value, c.sort_order yourtable (3, '' acted '', acted), (4, '' yoga '', (2) '' size '', (2) ('' A1 '', a1), (8), '' A2 '', a2) ('A3' ', A3)) c (sort_order, call, value) x pivot (for maximum p_id value) (' + cols + ')) sort order' order 'Execute (@query)   

view

all versions of the result:

  | Col | ID 1 | ID40 | ID 41 | ID42 | ---------------------------------------------- | ID | 1 | 40 | 41 | 42 | | Size | 3921 | 11979 | 11714 | 1159 9 | Worked. 39 | 301 | 28 9 265 | | Zodiac 690 5944.26 | 5060 | 4107.98 | | Avg1 | 17.69 | 19.75 | 17.51 ​​| 15.5 | | Avg2 | 0.18 | 0.5 | 0.43 | 0.35 | | A1 (Faucet) | 10000 | 10000 | 10000 | | A2 (Faucet) | 2000. 3000 5000 | | A3 (Faucet) | 1000 | 2000. 500    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -