如果我理解正確的話,下面給出:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FormData](
[formDataID] [int] IDENTITY(1,1) NOT NULL,
[formRowNumber] [int] NOT NULL,
[formColumnID] [int] NOT NULL,
[formDataDate] [datetime] NULL,
[formDataInt] [int] NULL,
[formDataChar] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[FormData] ON
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (1, 1, 1, NULL, NULL, N'John')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (2, 1, 2, NULL, NULL, N'Private')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (3, 1, 3, NULL, NULL, N'123456')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (4, 2, 1, NULL, NULL, N'Bill')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (5, 2, 2, NULL, NULL, N'Captain')
INSERT [dbo].[FormData] ([formDataID], [formRowNumber], [formColumnID], [formDataDate], [formDataInt], [formDataChar]) VALUES (6, 2, 3, NULL, NULL, N'789352')
SET IDENTITY_INSERT [dbo].[FormData] OFF
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[FormColumnDefinition](
[formColumnID] [int] IDENTITY(1,1) NOT NULL,
[formColumnDataType] [varchar](50) NOT NULL,
[formColumnName] [varchar](50) NOT NULL,
[formColumnLabel] [varchar](50) NOT NULL,
[formColumnSeqNumber] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[FormColumnDefinition] ON
INSERT [dbo].[FormColumnDefinition] ([formColumnID], [FormColumnDataType], [formColumnName], [formColumnLabel], [formColumnSeqNumber]) VALUES (1, N'System.String', N'Name', N'Name', 0)
INSERT [dbo].[FormColumnDefinition] ([formColumnID], [FormColumnDataType], [formColumnName], [formColumnLabel], [formColumnSeqNumber]) VALUES (2, N'String.String', N'Rank', N'Rank', 1)
INSERT [dbo].[FormColumnDefinition] ([formColumnID], [FormColumnDataType], [formColumnName], [formColumnLabel], [formColumnSeqNumber]) VALUES (3, N'System.Int32', N'SerialNumber', N'Serial Number', 2)
SET IDENTITY_INSERT [dbo].[FormColumnDefinition] OFF
你可以使用像轉動命令:
SELECT formRowNumber, [Name],[Rank],[SerialNumber]
FROM
(SELECT fcd.formColumnName, fd.formRowNumber, fd.formDataChar
FROM FormColumnDefinition fcd INNER JOIN FormData fd
ON fcd.formColumnID = fd.formColumnID) AS src
PIVOT
(
MAX(formDataChar)
FOR formColumnName IN ([Name],[Rank],[SerialNumber])
) AS pvt
ORDER BY pvt.formRowNumber
獲得:
formRowNumber Name Rank SerialNumber
1 John Private 123456
2 Bill Captain 789352
與PIVOT的問題是,你必須提前瞭解列名。如果你不介意一點動態SQL,你也可以解決這個問題。這裏有一個例子,我無恥地從Andras偷走了http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx。
DECLARE @cols NVARCHAR(2000);
SELECT @cols = STUFF((SELECT DISTINCT TOP 100 PERCENT
'],[' + formColumnName
FROM FormColumnDefinition
ORDER BY '],[' + formColumnName
FOR XML PATH('')
), 1, 2, '') + ']';
DECLARE @query NVARCHAR(4000);
SET @query = N'SELECT formRowNumber, ' + @cols +
'FROM (SELECT fcd.formColumnName, fd.formRowNumber, fd.formDataChar ' +
'FROM FormColumnDefinition fcd INNER JOIN FormData fd ' +
'ON fcd.formColumnID = fd.formColumnID) AS src PIVOT ' +
'(MAX(formDataChar) FOR formColumnName IN ('+ @cols + ')) AS pvt ORDER BY pvt.formRowNumber;';
EXECUTE(@query);
你有什麼試過的?一些示例數據和預期輸出將幫助我們能夠幫助您。 – 2010-06-09 17:28:29
你需要[動態SQL](http://www.sommarskog.se/dynamic_sql.html) – 2010-06-09 17:32:51