如何添加额外的行结构包含列名作为第一行?
How to add extra row structure contains columns name as first row?
问题
我在 SQL 服务器 2012 上工作,我遇到了无法添加包含列的行的问题
名称作为第一行意味着我需要将数据中的列名称显示为行
如您所见,所需结果我在行数据中添加了列名称
那么该怎么做?
这是当前结果
ItemId IPN PartnerName CustomerName Fan Motor Refrigator temprature
1 1233 Saico NULL NULL NULL NULL 55567
2 5433 Mbaby NULL 23444 NULL NULL NULL
3 590444 nagieb NULL NULL NULL 556666 NULL
要求或期望的结果
ItemId IPN PartnerName CustomerName Fan Motor Refrigator temprature
ItemId IPN PartnerName CustomerName Fan Motor Refrigator temprature
1 1233 Saico NULL NULL NULL NULL 55567
2 5433 Mbaby NULL 23444 NULL NULL NULL
3 590444 nagieb NULL NULL NULL 556666 NULL
下面的代码实现了我当前的结果
create table #temp
(
CustomerName nvarchar(200),
CustomerId nvarchar(50)
)
insert into #temp
(
CustomerId,
CustomerName
)
values
('1','Avidyne')
create table #FeatureTypes
(
FeatureId int,
FeatureName nvarchar(200)
)
insert into #FeatureTypes
(
FeatureId,
FeatureName
)
values
(1,'Fan'),
(2,'Refregator'),
(3,'Cars')
create table #Customer
(
CustomerId int,
CustomerName nvarchar(200)
)
insert into #Customer values
('1','Avidyne')
create table #Items
(
ItemId int,
CustomerId int,
IPN nvarchar(50),
PartnerName nvarchar(50)
)
insert into #Items (ItemId,CustomerId,IPN,PartnerName)
values
(1, 1,'1055','Magic'),
(2, 1,'4077','DataValidation'),
(3, 1,'3034','Moran')
create table #ItemFeatures
(
ItemId int,
FeatureId int,
FeatureValue nvarchar(50)
)
insert into #ItemFeatures (ItemId,FeatureId,FeatureValue)
values
(1, 1,'10'),
(2, 2,'40'),
(2, 1,'30'),
(1, 2,'20'),
(3, 1,'90'),
(1, 3,'180')
DECLARE @Columns as VARCHAR(MAX),@Header as VARCHAR(MAX)
SELECT @Columns =
COALESCE(@Columns + ', ','') + QUOTENAME(FeatureName)
FROM
(select distinct FeatureName from #FeatureTypes
) AS B
ORDER BY B.FeatureName
DECLARE @SQLs as VARCHAR(MAX)
SET @SQLs = 'SELECT cast(ItemId as nvarchar(50)),IPN,PartnerName,CustomerName,' + @Columns + '
FROM
(
select F.ItemId ,t.FeatureName,F.FeatureValue,I.IPN,I.PartnerName,FI.CustomerName
from #ItemFeatures F
Inner Join #Items I ON F.ItemId=I.ItemId
inner join #FeatureTypes T on T.FeatureId=F.FeatureId
inner join #temp FI on I.CustomerID=FI.CustomerID
) as PivotData
PIVOT
(
max(FeatureValue)
FOR FeatureName IN (' + @Columns + ')
) AS PivotResult
'
EXEC(@SQLs)
您需要 SELECT 'Cars' AS Cars, 'Fan' AS Fan, 'Refregator' AS Refregator
之类的东西,然后 union all
您现有的查询结果
这将生成 header 字符串
DECLARE @ColHeader as VARCHAR(MAX)
SELECT @ColHeader = COALESCE(@ColHeader + ', ','') + '''' + FeatureName + ''' AS ' + FeatureName
FROM
(
select distinct FeatureName from #FeatureTypes
) AS B
ORDER BY B.FeatureName
然后将您现有的查询更改为如下
SET @SQLs = 'SELECT ''ItemId'' as ItemId, ''IPN'' as IPN, ''PartnerName'' as PartnerName, ''CustomerName'' as CustomerName,' + @ColHeader + ' '
+ 'UNION ALL '
+ 'SELECT cast(ItemId as nvarchar(50)),IPN,PartnerName,CustomerName,' + @Columns + '
如果遇到错误,请执行 PRINT @SQLs
并检查查询
问题
我在 SQL 服务器 2012 上工作,我遇到了无法添加包含列的行的问题
名称作为第一行意味着我需要将数据中的列名称显示为行
如您所见,所需结果我在行数据中添加了列名称
那么该怎么做?
这是当前结果
ItemId IPN PartnerName CustomerName Fan Motor Refrigator temprature
1 1233 Saico NULL NULL NULL NULL 55567
2 5433 Mbaby NULL 23444 NULL NULL NULL
3 590444 nagieb NULL NULL NULL 556666 NULL
要求或期望的结果
ItemId IPN PartnerName CustomerName Fan Motor Refrigator temprature
ItemId IPN PartnerName CustomerName Fan Motor Refrigator temprature
1 1233 Saico NULL NULL NULL NULL 55567
2 5433 Mbaby NULL 23444 NULL NULL NULL
3 590444 nagieb NULL NULL NULL 556666 NULL
下面的代码实现了我当前的结果
create table #temp
(
CustomerName nvarchar(200),
CustomerId nvarchar(50)
)
insert into #temp
(
CustomerId,
CustomerName
)
values
('1','Avidyne')
create table #FeatureTypes
(
FeatureId int,
FeatureName nvarchar(200)
)
insert into #FeatureTypes
(
FeatureId,
FeatureName
)
values
(1,'Fan'),
(2,'Refregator'),
(3,'Cars')
create table #Customer
(
CustomerId int,
CustomerName nvarchar(200)
)
insert into #Customer values
('1','Avidyne')
create table #Items
(
ItemId int,
CustomerId int,
IPN nvarchar(50),
PartnerName nvarchar(50)
)
insert into #Items (ItemId,CustomerId,IPN,PartnerName)
values
(1, 1,'1055','Magic'),
(2, 1,'4077','DataValidation'),
(3, 1,'3034','Moran')
create table #ItemFeatures
(
ItemId int,
FeatureId int,
FeatureValue nvarchar(50)
)
insert into #ItemFeatures (ItemId,FeatureId,FeatureValue)
values
(1, 1,'10'),
(2, 2,'40'),
(2, 1,'30'),
(1, 2,'20'),
(3, 1,'90'),
(1, 3,'180')
DECLARE @Columns as VARCHAR(MAX),@Header as VARCHAR(MAX)
SELECT @Columns =
COALESCE(@Columns + ', ','') + QUOTENAME(FeatureName)
FROM
(select distinct FeatureName from #FeatureTypes
) AS B
ORDER BY B.FeatureName
DECLARE @SQLs as VARCHAR(MAX)
SET @SQLs = 'SELECT cast(ItemId as nvarchar(50)),IPN,PartnerName,CustomerName,' + @Columns + '
FROM
(
select F.ItemId ,t.FeatureName,F.FeatureValue,I.IPN,I.PartnerName,FI.CustomerName
from #ItemFeatures F
Inner Join #Items I ON F.ItemId=I.ItemId
inner join #FeatureTypes T on T.FeatureId=F.FeatureId
inner join #temp FI on I.CustomerID=FI.CustomerID
) as PivotData
PIVOT
(
max(FeatureValue)
FOR FeatureName IN (' + @Columns + ')
) AS PivotResult
'
EXEC(@SQLs)
您需要 SELECT 'Cars' AS Cars, 'Fan' AS Fan, 'Refregator' AS Refregator
之类的东西,然后 union all
您现有的查询结果
这将生成 header 字符串
DECLARE @ColHeader as VARCHAR(MAX)
SELECT @ColHeader = COALESCE(@ColHeader + ', ','') + '''' + FeatureName + ''' AS ' + FeatureName
FROM
(
select distinct FeatureName from #FeatureTypes
) AS B
ORDER BY B.FeatureName
然后将您现有的查询更改为如下
SET @SQLs = 'SELECT ''ItemId'' as ItemId, ''IPN'' as IPN, ''PartnerName'' as PartnerName, ''CustomerName'' as CustomerName,' + @ColHeader + ' '
+ 'UNION ALL '
+ 'SELECT cast(ItemId as nvarchar(50)),IPN,PartnerName,CustomerName,' + @Columns + '
如果遇到错误,请执行 PRINT @SQLs
并检查查询