2010-05-20 51 views
0

SQL 2005使用T-SQL,我想將行旋轉到列中。示例腳本:T SQL將行旋轉到列

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輸出 CPMeasuremnetGUID,Ink1,1級,Ink2,級別2,Ink3,Level3的....

+2

您使用PIVOT關鍵字。 – 2010-05-20 21:44:15

回答

0

這可處理高達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 
+0

你能否就你在做什麼做一些解釋? Unpivot和pivoting部分 – user291660 2010-05-21 13:17:31

+0

@cshah - 分別運行每個部分,您應該能夠看到發生了什麼。 UNPIVOT需要將InkName和InkLevel對轉換爲行,並根據添加到X子查詢中基礎數據的ROW_NUMBER生成的每對行保留數字。然後將這些行轉換爲PIVOT列。 – 2010-05-21 14:49:52