[英]SQL Server 2008 - Create a table dynamically from another table
I have a table like this:我有一张这样的桌子:
Column = NAME
Column = DATE
NAME | DATE | Y/N
John | 01/01/2012 | bit
Mary | 01/01/2012 | bit
James | 01/01/2012 | bit
John | 01/02/2012 | bit
Mary | 01/02/2012 | bit
James | 01/02/2012 | bit
John | 01/03/2012 | bit
Mary | 01/03/2012 | bit
James | 01/03/2012 | bit
I want to create some form of matrix or pivot so I end up with this:我想创建某种形式的矩阵或 pivot 所以我最终得到这个:
NAME | 01/01/2012 | 01/02/2012 | 01/03/2012
John | bit | bit | bit
Mary | bit | bit | bit
James | bit | bit | bit
I have seen some pivot examples that have a small amount of column items (like Banana, Apple, Orange) I need to have an indeterminate number of names and an indeterminate number of dates (so, no hard-coded column names).我已经看到一些 pivot 示例,其中包含少量列项(如 Banana、Apple、Orange),我需要不确定数量的名称和不确定数量的日期(因此,没有硬编码的列名称)。 I was thinking of splitting into multiple tables but I will always need to dynamically create either date columns or name columns.
我正在考虑拆分成多个表,但我总是需要动态创建日期列或名称列。
Can anyone help?谁能帮忙?
Maybe something like this:也许是这样的:
Test data测试数据
CREATE TABLE Table1
(
NAME VARCHAR(100),
[DATE] DATE,
[Y/N] BIT
)
INSERT INTO Table1
VALUES
('John','01/01/2012',1),
('Mary','01/01/2012',0),
('James','01/01/2012',1),
('John','01/02/2012',0),
('Mary','01/02/2012',1),
('James','01/02/2012',1),
('John','01/03/2012',1),
('Mary','01/03/2012',0),
('James','01/03/2012',0)
Finding unique columns查找唯一列
DECLARE @cols VARCHAR(MAX)
;WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY [DATE] ORDER BY [DATE]) AS RowNbr,
convert(varchar, [DATE], 103) AS [Date]
FROM
Table1
)
SELECT @cols=STUFF
(
(
SELECT
',' +QUOTENAME([Date])
FROM
CTE
WHERE
CTE.RowNbr=1
FOR XML PATH('')
)
,1,1,'')
Declare and executing dynamic sql声明并执行动态 sql
DECLARE @query NVARCHAR(4000)=
N'SELECT
*
FROM
(
SELECT
Table1.NAME,
CAST(Table1.[Y/N] AS INT) AS [Y/N],
convert(varchar, Table1.[DATE], 103) AS [Date]
FROM
Table1
) AS p
PIVOT
(
MAX([Y/N])
FOR [Date] IN ('+@cols+')
) AS pvt'
EXECUTE(@query)
Cleaning up after myself清理自己
DROP TABLE Table1
Result结果
Name 01/01/2012 02/01/2012 03/01/2012
James 1 1 0
John 1 0 1
Mary 0 1 0
If you have just a few columns you can use a static pivot:如果你只有几列,你可以使用 static pivot:
create table #temp
(
name varchar(50),
date datetime,
yesno bit
)
insert into #temp values('John', '01/01/2012', 1)
insert into #temp values('Mary', '01/01/2012', 1)
insert into #temp values('James', '01/01/2012', 1)
insert into #temp values('John', '01/02/2012', 0)
insert into #temp values('Mary', '01/02/2012', 0)
insert into #temp values('James', '01/02/2012', 0)
insert into #temp values('John', '01/03/2012', 1)
insert into #temp values('Mary', '01/03/2012', 0)
insert into #temp values('James', '01/03/2012', 1)
select name, [01/01/2012], [01/02/2012], [01/03/2012]
from
(
select name, date, cast(yesno as tinyint) as yesno
from #temp
) x
pivot
(
max(yesno)
for date in ([01/01/2012], [01/02/2012], [01/03/2012])
) p
drop table #temp
But it sounds like you want a dynamic pivot for this.但这听起来你想要一个动态的 pivot。 To do this you must first get the list of columns to pivot and then run your query:
为此,您必须首先获取 pivot 的列列表,然后运行查询:
create table test
(
name varchar(50),
date datetime,
yesno bit
)
insert into test values('John', '01/01/2012', 1)
insert into test values('Mary', '01/01/2012', 1)
insert into test values('James', '01/01/2012', 1)
insert into test values('John', '01/02/2012', 0)
insert into test values('Mary', '01/02/2012', 0)
insert into test values('James', '01/02/2012', 0)
insert into test values('John', '01/03/2012', 1)
insert into test values('Mary', '01/03/2012', 0)
insert into test values('James', '01/03/2012', 1)
DECLARE @cols AS VARCHAR(MAX),
@query AS VARCHAR(MAX);
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
'],[' + convert(varchar(10), t2.date, 101)
FROM test AS t2
ORDER BY '],[' + convert(varchar(10), t2.date, 101)
FOR XML PATH('')
), 1, 2, '') + ']'
set @query = 'select name, ' + @cols + '
from
(
select name, date, cast(yesno as tinyint) as yesno
from test
) x
pivot
(
max(yesno)
for date in (' + @cols + ')
) p'
execute(@query)
Here are some helpful links on dynamic pivots:以下是有关动态枢轴的一些有用链接:
Pivots with Dynamic Columns in SQL Server SQL 服务器中具有动态列的数据透视表
Using SQL Server 2005/2008 Pivot on Unknown Number of Columns (Dynamic Pivot) 在未知数量的列上使用 SQL Server 2005/2008 Pivot(动态数据透视)
there are a lot of questions/answers on SO for dynamic pivots.对于动态枢轴,有很多关于 SO 的问题/答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.