Sorry if this is covered elsewhere in another question, but I can't find any examples which exactly match what I need and I'm getting confused.
对不起,如果这个问题在其他问题的其他地方有所涉及,但我找不到任何与我需要的完全匹配的例子,我会感到困惑。
I have data in the table like this :-
我在表格中有这样的数据: -
Name | Value
---------------
John | Dog
John | Cat
John | Fish
Bob | Python
Bob | Camel
And I would like the data like this....
我希望这样的数据....
Name | Value_1 | Value_2 | Value_3
-------------------------------------
John | Dog | Cat | Fish
Bob | Python | Camel | NULL
I don't want to use case statements because the dog, cat, fish etc there are 20+ possible values and they could change overtime.
我不想使用案例陈述,因为狗,猫,鱼等有20多个可能的值,他们可以改变加班。
Anyone got any pointers?
任何人有任何指针?
3 个解决方案
#1
3
This comes close to what you're after. i've also included a table creation script and sample data for others to use. Inspiration for this solution came from here
这接近你所追求的。我还包括一个表创建脚本和其他人可以使用的示例数据。这个解决方案的灵感来自这里
-- Dynamic PIVOT
DECLARE @T AS TABLE(y INT NOT NULL PRIMARY KEY);
DECLARE
@cols AS NVARCHAR(MAX),
@y AS INT,
@sql AS NVARCHAR(MAX);
-- Construct the column list for the IN clause
-- e.g., [Dog],[Python]
SET @cols = STUFF(
(SELECT N',' + QUOTENAME(y) AS [text()]
FROM (SELECT DISTINCT [Value] AS y FROM dbo.table_1) AS Y
ORDER BY y
FOR XML PATH('')),
1, 1, N'');
-- Construct the full T-SQL statement
-- and execute dynamically
SET @sql = N'SELECT *
FROM (SELECT *
FROM dbo.table_1) AS D
PIVOT(MIN(value) FOR value IN(' + @cols + N')) AS P;';
PRINT @sql
EXEC sp_executesql @sql;
GO
Table creation:
CREATE TABLE [dbo].[Table_1](
[Name] [varchar](50) COLLATE Latin1_General_CI_AS NULL,
[Value] [varchar](50) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
Sample data:
insert into dbo.table_1 values ('John','Dog')
insert into dbo.table_1 values ('John','Cat')
insert into dbo.table_1 values ('John','Fish')
insert into dbo.table_1 values ('Bob ','Python')
insert into dbo.table_1 values ('Bob ','Camel')
#2
0
This question is somewhat similar:
这个问题有点类似:
Cross Tab - Storing different dates (Meeting1, Meeting2, Meeting 3 etc) in the same column
交叉表 - 在同一列中存储不同的日期(Meeting1,Meeting2,Meeting 3等)
#3
0
Take a look at this :
看看这个 :
Might be helpful ..
可能会有所帮助..
#1
3
This comes close to what you're after. i've also included a table creation script and sample data for others to use. Inspiration for this solution came from here
这接近你所追求的。我还包括一个表创建脚本和其他人可以使用的示例数据。这个解决方案的灵感来自这里
-- Dynamic PIVOT
DECLARE @T AS TABLE(y INT NOT NULL PRIMARY KEY);
DECLARE
@cols AS NVARCHAR(MAX),
@y AS INT,
@sql AS NVARCHAR(MAX);
-- Construct the column list for the IN clause
-- e.g., [Dog],[Python]
SET @cols = STUFF(
(SELECT N',' + QUOTENAME(y) AS [text()]
FROM (SELECT DISTINCT [Value] AS y FROM dbo.table_1) AS Y
ORDER BY y
FOR XML PATH('')),
1, 1, N'');
-- Construct the full T-SQL statement
-- and execute dynamically
SET @sql = N'SELECT *
FROM (SELECT *
FROM dbo.table_1) AS D
PIVOT(MIN(value) FOR value IN(' + @cols + N')) AS P;';
PRINT @sql
EXEC sp_executesql @sql;
GO
Table creation:
CREATE TABLE [dbo].[Table_1](
[Name] [varchar](50) COLLATE Latin1_General_CI_AS NULL,
[Value] [varchar](50) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO
Sample data:
insert into dbo.table_1 values ('John','Dog')
insert into dbo.table_1 values ('John','Cat')
insert into dbo.table_1 values ('John','Fish')
insert into dbo.table_1 values ('Bob ','Python')
insert into dbo.table_1 values ('Bob ','Camel')
#2
0
This question is somewhat similar:
这个问题有点类似:
Cross Tab - Storing different dates (Meeting1, Meeting2, Meeting 3 etc) in the same column
交叉表 - 在同一列中存储不同的日期(Meeting1,Meeting2,Meeting 3等)
#3
0
Take a look at this :
看看这个 :
Might be helpful ..
可能会有所帮助..