I have a table that looks like this
OutputTablesID Label ColumnOrder
236 text 1
236 text 2
236 text 3
. . .
. . .
. . .
236 text 25
I need the table to look like this
OutputTablesID 1>>2>>3>>4>>5>>6>>7>>8>>9>>10>>11>>12>>13>>14>>15
236>>>>>>>>>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
I have tried code that I used from an existing pivot table I have but I cannot use Label in the aggregate function because it is a text string.
This is my attempt at the pivot table
Create FUNCTION [dbo].[ColOrder]
(
)
RETURNS TABLE
AS
RETURN
(
SELECT OutputTablesId, Label, 1,2,3,4,5,6,7,8,9,10,11,12
from
(
SELECT OCL.Label, OCL.OutputTablesId
FROM OCL
) AS D
PIVOT
(
sum(Label)
FOR ColumnOrder IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
) AS P
Thank you for your comments and suggestions!
Your code is very close but you are trying to use a
sum()on avarcharwhich will not work. If you change the aggregate function tomax()then it should work:See SQL Fiddle with Demo
If you want to create this as a function, then you can use:
See SQL Fiddle with Demo
The result is: