在创建数据透视表期间将列名转换为特定字符 - SQL

时间:2016-06-13 14:40:20

标签: sql sql-server

我有一个表格测试,其中有一个列标签。它的数据长度超过50个字符。

当我从'测试'创建数据透视表时表,它使用那些长字符数据作为列名。

我的要求是用户强制转换函数将列名限制为26个字符。

我使用下面的脚本,但它不能按预期工作。

create table V_Test as
select * from 
    (select * from Test) x
pivot (sum(Average) for Label in (
    S03_CreatePlansdadsada,
    S03_CreatePlan_T01_NavigateTosdsadsaded,
    S03_CreatePlan_T03_abcdefgmanagementsdasda,
    S03_CreatePlan_T16_SetStatusToOngoingasdasda,
    S03_CreatePlan_T17_Ldsdssdadsadas                           
)
) p

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

由于标签名称无论如何都是硬编码的,因此使用别名应该没问题。

在MS SQL Server上,它看起来像这样 只是使用变量表和临时表进行演示。

Intl

这将返回以下结果:

DECLARE @Test TABLE (PlanID int, Label varchar(50), Average int);
insert into @Test values 
(1,'S03_CreatePlansdadsada',10),
(1,'S03_CreatePlan_T01_NavigateTosdsadsaded',20),
(1,'S03_CreatePlan_T03_abcdefgmanagementsdasda',30),
(1,'S03_CreatePlan_T16_SetStatusToOngoingasdasda',40),
(1,'S03_CreatePlan_T17_Ldsdssdadsadas',50),
(1,'S03_CreatePlansdadsada',60),
(1,'S03_CreatePlan_T01_NavigateTosdsadsaded',70),
(1,'S03_CreatePlan_T03_abcdefgmanagementsdasda',80),
(1,'S03_CreatePlan_T16_SetStatusToOngoingasdasda',90),
(1,'S03_CreatePlan_T17_Ldsdssdadsadas',100);

IF OBJECT_ID('tempdb..#tmpTest') IS NOT NULL DROP TABLE #tmpTest;

select 
PlanID,
S03_CreatePlansdadsada as CreatePlans,
S03_CreatePlan_T01_NavigateTosdsadsaded as T01_NavigateTo,
S03_CreatePlan_T03_abcdefgmanagementsdasda as T03_managements,
S03_CreatePlan_T16_SetStatusToOngoingasdasda as T16_SetStatusToOng,
S03_CreatePlan_T17_Ldsdssdadsadas as T17_Lsd
into #tmpTest
from @Test
pivot (sum(Average) for Label in (
    S03_CreatePlansdadsada,
    S03_CreatePlan_T01_NavigateTosdsadsaded,
    S03_CreatePlan_T03_abcdefgmanagementsdasda,
    S03_CreatePlan_T16_SetStatusToOngoingasdasda,
    S03_CreatePlan_T17_Ldsdssdadsadas                           
    )
) p;

select * from  #tmpTest;

动态方式有点复杂,因为SQL需要构造才能执行。 (注意代码注入)

PlanID  CreatePlans T01_NavigateTo  T03_managements T16_SetStatusToOng  T17_Lsd
1           70          90          110             130                 150

对于Oracle数据库,它看起来像这样:

IF OBJECT_ID('tempdb..#tmpTestData') IS NOT NULL DROP TABLE #tmpTestData;
CREATE TABLE #tmpTestData (PlanID int, Label varchar(50), Average int);

insert into #tmpTestData values 
(1,'S03_CreatePlansdadsada',10),
(1,'S03_CreatePlan_T01_NavigateTosdsadsaded',20),
(1,'S03_CreatePlan_T03_abcdefgmanagementsdasda',30),
(1,'S03_CreatePlan_T16_SetStatusToOngoingasdasda',40),
(1,'S03_CreatePlan_T17_Ldsdssdadsadas',50),
(1,'S03_CreatePlansdadsada',60),
(1,'S03_CreatePlan_T01_NavigateTosdsadsaded',70),
(1,'S03_CreatePlan_T03_abcdefgmanagementsdasda',80),
(1,'S03_CreatePlan_T16_SetStatusToOngoingasdasda',90),
(1,'S03_CreatePlan_T17_Ldsdssdadsadas',100);

declare @Columns nvarchar(max);
declare @AliasedColumns nvarchar(max);
set  @Columns = STUFF((SELECT ', ' + QUOTENAME(Label) FROM  #tmpTestData GROUP BY Label FOR  XML PATH(''), TYPE).value('.', 'NVARCHAR(max)') ,1,1,'');
set  @AliasedColumns = STUFF((SELECT ', ' + QUOTENAME(Label) +' as '+ QUOTENAME(substring(Label,5,14)) FROM  #tmpTestData GROUP BY Label FOR  XML PATH(''), TYPE).value('.', 'NVARCHAR(max)') ,1,1,'');

declare @SQL nvarchar(max);

IF OBJECT_ID('tempdb..##tmpPivotTest') IS NOT NULL DROP TABLE ##tmpPivotTest;
set @SQL = 'select PlanID, '+ @AliasedColumns + ' 
into ##tmpPivotTest
from #tmpTestData pivot (sum(Average) for Label in ('+ @Columns +')) p';

--select @SQL;
exec (@SQL);

select * from ##tmpPivotTest;
IF OBJECT_ID('tempdb..##tmpPivotTest') IS NOT NULL DROP TABLE ##tmpPivotTest;