我一直在测试枢轴,这似乎首先做了我想要的。将foto元数据名称(longname)设置为Model,ExiffVersion以及更多作为列名称和值(元数据)作为行值以及fotoId。有超过3000行。数据透视为此查询提供相同的金额,每行有1个属性结果。
表格结构:
CREATE TABLE [dbo].[FotoMetaData](
[id] [int] IDENTITY(1,1) NOT NULL,
[validFrom] [date] NULL,
[validUntil] [date] NULL,
[alteredOn] [datetime] NULL,
[isDeleted] [bit] NULL,
[fotoId] [int] NOT NULL,
[longname] [nvarchar](25) NOT NULL,
[metadata] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_FotoMetaData] PRIMARY KEY CLUSTERED
(
[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] TEXTIMAGE_ON [PRIMARY]
GO
示例数据(由于长列表而提取1个具有有限属性的照片):
SET IDENTITY_INSERT [dbo].[FotoMetaData] ON
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (233, NULL, NULL, NULL, NULL, 1, N'ImageDescription', N' ')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (234, NULL, NULL, NULL, NULL, 1, N'Make', N'NIKON')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (235, NULL, NULL, NULL, NULL, 1, N'Model', N'COOLPIX S9700 ')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (236, NULL, NULL, NULL, NULL, 1, N'Orientation', N'1')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (237, NULL, NULL, NULL, NULL, 1, N'XResolution', N'300')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (238, NULL, NULL, NULL, NULL, 1, N'YResolution', N'300')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (239, NULL, NULL, NULL, NULL, 1, N'ResolutionUnit', N'2')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (240, NULL, NULL, NULL, NULL, 1, N'Software', N'COOLPIX S9700V1.0 ')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (241, NULL, NULL, NULL, NULL, 1, N'DateTime', N'2014:09:21 13:44:31')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (242, NULL, NULL, NULL, NULL, 1, N'YCbCrPositioning', N'2')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (243, NULL, NULL, NULL, NULL, 1, N'ExposureTime', N'0,0008')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (244, NULL, NULL, NULL, NULL, 1, N'FNumber', N'3,7')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (245, NULL, NULL, NULL, NULL, 1, N'ExposureProgram', N'2')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (246, NULL, NULL, NULL, NULL, 1, N'ISOSpeedRatings', N'125')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (247, NULL, NULL, NULL, NULL, 1, N'Unknown', N'1')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (248, NULL, NULL, NULL, NULL, 1, N'ImageDescription', N' ')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (249, NULL, NULL, NULL, NULL, 1, N'Make', N'NIKON')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (250, NULL, NULL, NULL, NULL, 1, N'Model', N'COOLPIX S9700 ')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (251, NULL, NULL, NULL, NULL, 1, N'Orientation', N'1')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (252, NULL, NULL, NULL, NULL, 1, N'XResolution', N'300')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (253, NULL, NULL, NULL, NULL, 1, N'YResolution', N'300')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (254, NULL, NULL, NULL, NULL, 1, N'ResolutionUnit', N'2')
GO
INSERT [dbo].[FotoMetaData] ([id], [validFrom], [validUntil], [alteredOn], [isDeleted], [fotoId], [longname], [metadata]) VALUES (255, NULL, NULL, NULL, NULL, 1, N'Software', N'COOLPIX S9700V1.0 ')
GO
SET IDENTITY_INSERT [dbo].[FotoMetaData] OFF
GO
PIVOT:
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(longname)FROM (SELECT DISTINCT longname FROM FotoMetaData) AS fotoMetaData
SET @DynamicPivotQuery = N'SELECT fotoId, ' + @ColumnName + '
FROM FotoMetaData PIVOT(MAX(metadata) FOR longname IN (' + @ColumnName + ')) AS PVTTable'
EXEC sp_executesql @DynamicPivotQuery
我希望每个foto(fotoId)都能在同一行填充所有属性。有11个foto,所以我想要11行。我知道我必须在fotoId做一些小组,但我不知道,因为我不太了解PIVOT声明。我已经改编了一个我在别处找到动态支点的例子。
答案 0 :(得分:2)
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX), @MaxColumn AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(longname)FROM (SELECT DISTINCT longname FROM FotoMetaData) AS fotoMetaData
SELECT @MaxColumn= ISNULL(@MaxColumn + ',Max(','')
+ QUOTENAME(longname) +') AS '+ QUOTENAME(longname) FROM (SELECT DISTINCT longname FROM FotoMetaData) AS fotoMetaData
SET @DynamicPivotQuery = N'SELECT fotoId,MAX(' + @MaxColumn + ' FROM(SELECT fotoId,' + @ColumnName + '
FROM FotoMetaData
PIVOT(MAX(metadata) FOR longname IN (' + @ColumnName + ')) AS PVTTable)a GROUP BY fotoId '
EXEC sp_executesql @DynamicPivotQuery
这会给你结果,但我不知道有什么简单的方法......