如何在 SQL Server 中旋转文本列?
Posted
技术标签:
【中文标题】如何在 SQL Server 中旋转文本列?【英文标题】:How to pivot text columns in SQL Server? 【发布时间】:2012-04-19 00:51:11 【问题描述】:我的数据库中有一个这样的表 (SQL Server 2008)
ID Type Desc
--------------------------------
C-0 Assets No damage
C-0 Environment No impact
C-0 People No injury or health effect
C-0 Reputation No impact
C-1 Assets Slight damage
C-1 Environment Slight environmental damage
C-1 People First Aid Case (FAC)
C-1 Reputation Slight impact; Compaints from local community
我必须将 Assets、People、Environment 和 Reputation 显示为列,并将匹配的 Desc 显示为值。但是当我运行数据透视查询时,我所有的值都是空的。
有人可以查看我的查询并告诉我哪里做错了吗?
Select severity_id,pt.[1] As People, [2] as Assets , [3] as Env, [4] as Rep
FROM
(
select * from COMM.Consequence
) As Temp
PIVOT
(
max([DESCRIPTION])
FOR [TYPE] In([1], [2], [3], [4])
) As pt
这是我的输出
ID People Assets Env Rep
-----------------------------------
C-0 NULL NULL NULL NULL
C-1 NULL NULL NULL NULL
C-2 NULL NULL NULL NULL
C-3 NULL NULL NULL NULL
C-4 NULL NULL NULL NULL
C-5 NULL NULL NULL NULL
【问题讨论】:
【参考方案1】:我在 sql server 中重新创建了它,它工作得很好。
当人们不知道 TYPE 和 DESCRIPTION 列中的内容是什么时,我正在尝试将其转换为工作。
我也以此为指导。 (Convert Rows to columns using 'Pivot' in SQL Server)
编辑----
这是我对上述问题的解决方案,您不知道任一字段中的内容....
-- setup commands
drop table #mytemp
go
create table #mytemp (
id varchar(10),
Metal_01 varchar(30),
Metal_02 varchar(100)
)
-- insert the data
insert into #mytemp
select 'C-0','Metal One','Metal_One' union all
select 'C-0','Metal & Two','Metal_Two' union all
select 'C-1','Metal One','Metal_One' union all
select 'C-1','Metal (Four)','Metal_Four' union all
select 'C-2','Metal (Four)','Metal_Four' union all
select 'C-2','Metal / Six','Metal_Six' union all
select 'C-3','Metal Seven','Metal_Seven' union all
select 'C-3','Metal Eight','Metal_Eight'
-- prepare the data for rotating:
drop table #mytemp_ReadyForRotate
select *,
replace(
replace(
replace(
replace(
replace(
mt.Metal_01,space(1),'_'
)
,'(','_'
)
,')','_'
)
,'/','_'
)
,'&','_'
)
as Metal_No_Spaces
into #mytemp_ReadyForRotate
from #mytemp mt
select 'This is the content of "#mytemp_ReadyForRotate"' as mynote, * from #mytemp_ReadyForRotate
-- this is for when you KNOW the content:
-- in this query I am able to put the content that has the punctuation in the cell under the appropriate column header
Select id, pt.Metal_One, Metal_Two, Metal_Four, Metal_Six, Metal_Seven,Metal_Eight
FROM
(
select * from #mytemp
) As Temp
PIVOT
(
max(Metal_01)
FOR Metal_02 In(
Metal_One,
Metal_Two,
Metal_Four,
Metal_Six,
Metal_Seven,
Metal_Eight
)
) As pt
-- this is for when you DON'T KNOW the content:
-- in this query I am UNABLE to put the content that has the punctuation in the cell under the appropriate column header
-- unknown as to why it gives me so much grief - just can't get it to work like the above
-- it WORKS just fine but not with the punctuation field
drop table ##csr_Metals_Rotated
go
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@InsertIntoTempTable as nvarchar(4000)
select @cols = STUFF((SELECT ',' + QUOTENAME(Metal_No_Spaces)
from #mytemp_ReadyForRotate
group by Metal_No_Spaces
order by Metal_No_Spaces
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT id,' + @cols + ' into ##csr_Metals_Rotated from
(
select id as id, Metal_No_Spaces
from #mytemp_ReadyForRotate
) x
pivot
(
max(Metal_No_Spaces)
for Metal_No_Spaces in (' + @cols + ')
) p '
execute(@query);
select * from ##csr_Metals_Rotated
【讨论】:
虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。 我必须道歉 - 我的“答案”没有回答问题。 - 我只需要发布的帖子答案是动态的......程序员不知道字段的内容,但 sql server 可以动态创建字段并填充它们。 -- 我已经弄明白了,我会在几天后在这里发布我的发现 - 现在时间紧迫。【参考方案2】:Select severity_id, pt.People, Assets, Environment, Reputation
FROM
(
select * from COMM.Consequence
) As Temp
PIVOT
(
max([DESCRIPTION])
FOR [TYPE] In([People], [Assets], [Environment], [Reputation])
) As pt
【讨论】:
我很好奇为什么在第一个 Select pt.People 中使用了,但是 pt.People 被使用了。这里不需要前缀 如果你不想在结果中显示severity_id怎么办? @SrJefers 在派生表中使用查询并在主查询中获取所需的列。以上是关于如何在 SQL Server 中旋转文本列?的主要内容,如果未能解决你的问题,请参考以下文章
如何保持某些列原样并动态旋转 SQL Server 2012 中的最后一列 [重复]
如何在 SQL Server 2008 中转换文本字符串中的哈希密码