views:

58

answers:

2

SQL 2005 using T-SQL, I want to rotate rows into columns. Sample script:

Use TempDB
Go

CREATE TABLE [dbo].[CPPrinter_InkLevels](
    [CPPrinter_InkLevels_ID] [int] IDENTITY(1,1) NOT NULL,
    [CPMeasurementGUID] [uniqueidentifier] NOT NULL,
    [InkName] [varchar](30) NOT NULL,
    [InkLevel] [decimal](6, 2) NOT NULL,
 CONSTRAINT [PK_CPPrinter_InkLevels] PRIMARY KEY CLUSTERED 
(
    [CPPrinter_InkLevels_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

SET IDENTITY_INSERT [dbo].[CPPrinter_InkLevels] ON
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (1, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Black', CAST(0.60 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (2, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Cyan', CAST(0.69 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (3, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Magenta', CAST(0.55 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (4, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Yellow', CAST(0.51 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (5, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Light Black', CAST(0.64 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (6, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Light Cyan', CAST(0.43 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (7, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Light Magenta', CAST(0.30 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (8, N'6acc1562-4e02-45ff-b480-9e01fb97fccf', N'Waste Tank', CAST(0.18 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (9, N'932348a7-6e2f-4a10-9760-be1ae640c7d7', N'Black', CAST(0.60 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (10, N'932348a7-6e2f-4a10-9760-be1ae640c7d7', N'Cyan', CAST(0.69 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (11, N'932348a7-6e2f-4a10-9760-be1ae640c7d7', N'Magenta', CAST(0.55 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (12, N'932348a7-6e2f-4a10-9760-be1ae640c7d7', N'Yellow', CAST(0.51 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (13, N'932348a7-6e2f-4a10-9760-be1ae640c7d7', N'Light Black', CAST(0.64 AS Decimal(6, 2)))
INSERT [dbo].[CPPrinter_InkLevels] ([CPPrinter_InkLevels_ID], [CPMeasurementGUID], [InkName], [InkLevel]) VALUES (14, N'932348a7-6e2f-4a10-9760-be1ae640c7d7', N'Light Cyan', CAST(0.43 AS Decimal(6, 2)))
Go

SELECT * FROM [dbo].[CPPrinter_InkLevels]

--Desired output CPMeasuremnetGUID, Ink1, Level1, Ink2, Level2, Ink3, Level3....

A: 

This handles up to 10:

SELECT  *
FROM    (
         SELECT CPMeasurementGUID
               ,col + CAST(Seq AS varchar) AS colname
               ,val
         FROM   (
                 SELECT CPMeasurementGUID
                       ,ROW_NUMBER() OVER (PARTITION BY CPMeasurementGUID ORDER BY CPPrinter_InkLevels_ID) AS Seq
                       ,CAST(InkName AS varchar) AS Ink
                       ,CAST(InkLevel AS varchar) AS Level
                 FROM   [dbo].[CPPrinter_InkLevels]
                ) AS X UNPIVOT ( val FOR col IN ([Ink], [Level]) ) AS unpvt
        ) AS Y PIVOT ( MAX(val) FOR colname IN ([Ink1], [Level1], [Ink2], [Level2], [Ink3], [Level3], [Ink4], [Level4],
                                                [Ink5], [Level5], [Ink6], [Level6], [Ink7], [Level7], [Ink8], [Level8],
                                                [Ink9], [Level9], [Ink10], [Level10]) ) AS pvt
Cade Roux
Can you proivide little explanation on what are you doing? Unpivot and pivoting part
@cshah - Run each part individually, you should be able to see what is going on. The UNPIVOT needs to turn the pairs of InkName and InkLevel columns into rows, keeping the numbers together for each pair of rows produced based on the ROW_NUMBER added to the underlying data in the X subquery. Then the rows are turned into columns with the PIVOT.
Cade Roux