SQL2005 中的动态透视需要帮助

发布于 2024-07-17 12:24:46 字数 312 浏览 10 评论 0原文

我有一个名称值对表,我在其中存储标签:

TAGID | NAME | VALUE

我有一个此标签适用的“事物”表,

THINGID | TAGID

我需要一个查询来生成结果集,其中列/字段都是可能的标签名称(名称字段在标签表)对于给定的 THINGID 和值是相应的标签值。

THINGID | TAGNAME1 | TAGNAME2 | ... |etc.

我可以找到带有固定列的示例,但没有这样的。

I have a table of name value pairs where I am storing tags:

TAGID | NAME | VALUE

I have a table of 'Things' this tags apply to

THINGID | TAGID

I need a query to generate a resultSet were the columns/fields are all possible TAG-NAMES (NAME field in the TAG table)for a given THINGID and the values are the correspondent tag values.

THINGID | TAGNAME1 | TAGNAME2 | ... |etc.

I can find examples with fixed columns but nothing like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2024-07-24 12:24:46

与略有不同的架构(thing_id,tag_name,tag_value)类似的东西 - 没有标签表:

CREATE PROCEDURE dbo.PivotData(@table VARCHAR(128), @basefield
VARCHAR(128), @namefield VARCHAR(128), @valuefield VARCHAR(128))
AS
DECLARE
 @sql NVARCHAR(MAX)
SET @sql =
 'DECLARE
 @colName VARCHAR(128),
 @sqlBegin NVARCHAR(MAX),
 @sqlMiddle NVARCHAR(MAX),
 @sqlEnd NVARCHAR(MAX),
 @counter INT
 SET @counter = 1
 SET @sqlBegin = N''SELECT DISTINCT t0.'' + ''' + QUOTENAME(@basefield) + '''
 SET @sqlMiddle = N'' FROM '' + ''' + QUOTENAME(@table) + ''' + '' AS t0 ''
 DECLARE cols CURSOR FOR
 SELECT DISTINCT TOP 100 PERCENT ' + QUOTENAME(@namefield) + '
 FROM ' + QUOTENAME(@table) + '
 WHERE ' + QUOTENAME(@basefield) + ' IS NOT NULL
 ORDER BY ' + QUOTENAME(@namefield) + '
 OPEN cols
 FETCH NEXT FROM cols INTO @colName
 WHILE @@FETCH_STATUS = 0 BEGIN
 SET @sqlBegin = @sqlBegin + '', t'' + CAST(@counter AS VARCHAR) +
''.'' + ''' + QUOTENAME(@valuefield) + ''' + '' AS '' +
QUOTENAME(@colName) + ''''
 SET @sqlMiddle = @sqlMiddle + '' LEFT OUTER JOIN '' + ''' +
  QUOTENAME(@table) + ''' + '' AS t'' + CAST(@counter AS VARCHAR) +
'' ON t0.'' + ''' +
  QUOTENAME(@basefield) + ''' + '' = t'' + CAST(@counter AS VARCHAR)
+ ''.'' + ''' +
  QUOTENAME(@basefield) + ''' + '' AND t'' + CAST(@counter AS
VARCHAR) + ''.'' + ''' + QUOTENAME(@namefield) + ''' + ''='' +
QUOTENAME(@colName, '''''''') + ''''
 SET @counter = @counter + 1
 FETCH NEXT FROM cols INTO @colName
 END
 CLOSE cols
 DEALLOCATE cols
 SET @sqlEnd = '' WHERE t0.'' + ''' + QUOTENAME(@basefield) + ''' + ''
IS NOT NULL''
 DECLARE @sql NVARCHAR(MAX)
 SET @sql = @sqlBegin + @sqlMiddle + @sqlEnd
 EXEC sp_executesql @sql'
EXEC sp_executesql @sql
RETURN 0
GO
CREATE TABLE test_data (
id int identity primary key,
person_id int,
person_data_field VARCHAR (128),
person_data_value VARCHAR (128)
)
GO
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (1, 'Name', 'John')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (1, 'Surname', 'Smith')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (1, 'Email', '[email protected]')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (2, 'Name', 'Sarah')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (2, 'Surname', 'Lee')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (2, 'Phone', '012345678')
GO
EXEC [dbo].[PivotData] @table='test_data',
@basefield='person_id', @namefield='person_data_field', @valuefield='person_data_value'
GO

我发现这工作得太慢并停止进一步调整它,但它有点符合您的要求。

Something similar with slightly different schema (thing_id, tag_name, tag_value) - without tags table:

CREATE PROCEDURE dbo.PivotData(@table VARCHAR(128), @basefield
VARCHAR(128), @namefield VARCHAR(128), @valuefield VARCHAR(128))
AS
DECLARE
 @sql NVARCHAR(MAX)
SET @sql =
 'DECLARE
 @colName VARCHAR(128),
 @sqlBegin NVARCHAR(MAX),
 @sqlMiddle NVARCHAR(MAX),
 @sqlEnd NVARCHAR(MAX),
 @counter INT
 SET @counter = 1
 SET @sqlBegin = N''SELECT DISTINCT t0.'' + ''' + QUOTENAME(@basefield) + '''
 SET @sqlMiddle = N'' FROM '' + ''' + QUOTENAME(@table) + ''' + '' AS t0 ''
 DECLARE cols CURSOR FOR
 SELECT DISTINCT TOP 100 PERCENT ' + QUOTENAME(@namefield) + '
 FROM ' + QUOTENAME(@table) + '
 WHERE ' + QUOTENAME(@basefield) + ' IS NOT NULL
 ORDER BY ' + QUOTENAME(@namefield) + '
 OPEN cols
 FETCH NEXT FROM cols INTO @colName
 WHILE @@FETCH_STATUS = 0 BEGIN
 SET @sqlBegin = @sqlBegin + '', t'' + CAST(@counter AS VARCHAR) +
''.'' + ''' + QUOTENAME(@valuefield) + ''' + '' AS '' +
QUOTENAME(@colName) + ''''
 SET @sqlMiddle = @sqlMiddle + '' LEFT OUTER JOIN '' + ''' +
  QUOTENAME(@table) + ''' + '' AS t'' + CAST(@counter AS VARCHAR) +
'' ON t0.'' + ''' +
  QUOTENAME(@basefield) + ''' + '' = t'' + CAST(@counter AS VARCHAR)
+ ''.'' + ''' +
  QUOTENAME(@basefield) + ''' + '' AND t'' + CAST(@counter AS
VARCHAR) + ''.'' + ''' + QUOTENAME(@namefield) + ''' + ''='' +
QUOTENAME(@colName, '''''''') + ''''
 SET @counter = @counter + 1
 FETCH NEXT FROM cols INTO @colName
 END
 CLOSE cols
 DEALLOCATE cols
 SET @sqlEnd = '' WHERE t0.'' + ''' + QUOTENAME(@basefield) + ''' + ''
IS NOT NULL''
 DECLARE @sql NVARCHAR(MAX)
 SET @sql = @sqlBegin + @sqlMiddle + @sqlEnd
 EXEC sp_executesql @sql'
EXEC sp_executesql @sql
RETURN 0
GO
CREATE TABLE test_data (
id int identity primary key,
person_id int,
person_data_field VARCHAR (128),
person_data_value VARCHAR (128)
)
GO
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (1, 'Name', 'John')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (1, 'Surname', 'Smith')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (1, 'Email', '[email protected]')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (2, 'Name', 'Sarah')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (2, 'Surname', 'Lee')
INSERT INTO dbo.test_data (person_id, person_data_field, person_data_value)
VALUES (2, 'Phone', '012345678')
GO
EXEC [dbo].[PivotData] @table='test_data',
@basefield='person_id', @namefield='person_data_field', @valuefield='person_data_value'
GO

I figured out that this works way too slow and stopped tuning it further, but it sort of works up to your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文