Добавить Featurevalue без повторного ItemId в сводной таблице? - PullRequest
2 голосов
/ 04 февраля 2020

Как добавить значение компонента без повторяющегося ItemId в сводную таблицу?

Я работаю на SQL server 2012.

Я создаю сводную таблицу на основе itemId, но при добавлении данных Featurevalue повторяется

так, как предотвратить повторение ItemId?

желаемый результат

ItemCode  IPN    PartnerName CustomerName   Fan Refrigator  temprature  FeatureValue
1     1233   Saico        Michel        1        2                 1            1234          
2     5433   Mbaby        Michel        0        1                 0            7777
3     44333  sadeoMany    Michel        1        0                 1            88888

Что я пробовал:

create table #InputData
(
CustomerID uniqueidentifier

)
insert into #InputData values ('0ce19920-f0ca-433c-abb1-4e84d52b618b'),('188b8053-18c0-4092-955e-962f54485e43')

create table #customers
(
CustomerID uniqueidentifier,
CustomerName  nvarchar(200)

)
insert into #customers 
values
('0ce19920-f0ca-433c-abb1-4e84d52b618b','Michel')

create table #FeatureType
(
FeatureId int,
FeatureName  nvarchar(200)

)
insert into #FeatureType 
values
(1,'temprature'),
(2,'Fan'),
(3,'Refrigator')

create table #Items
(
ItemId int,
IPN  nvarchar(200),
PartnerPart  nvarchar(200),
PartnerName nvarchar(100)
)
insert into #Items 
values
(1,'1233','Mobilic','Saico'),
(2,'5433','Saldom','Mbaby'),
(3,'44333','Silicon','sadeoMany')

create table #ItemFeatures
(
ItemFeatureId int,
ItemId  int,
FeatureId int,
CustomerId uniqueidentifier,
FeatureValue  nvarchar(50)
)
insert into #ItemFeatures 
values
(1,1,1,'0ce19920-f0ca-433c-abb1-4e84d52b618b','1234'),
(2,1,2,'0ce19920-f0ca-433c-abb1-4e84d52b618b','4333'),
(3,1,3,'0ce19920-f0ca-433c-abb1-4e84d52b618b','55555'),
(4,1,3,'0ce19920-f0ca-433c-abb1-4e84d52b618b','66666'),
(5,2,3,'0ce19920-f0ca-433c-abb1-4e84d52b618b','7777'),


DECLARE @Columns as VARCHAR(MAX)
SELECT @Columns =
COALESCE(@Columns + ', ','') + QUOTENAME(FeatureName)
FROM
--distinct FT.FeatureName 
(select  distinct FT.FeatureName  from #InputData Feat inner join #ItemFeatures ItemF
on ItemF.CustomerId=Feat.CustomerId INNER join #FeatureType FT on ItemF.FeatureId=FT.FeatureId

   ) AS B
   ORDER BY B.FeatureName

DECLARE @SQLs as VARCHAR(MAX)

SET @SQLs = 'SELECT ItemCode, IPN,PartnerName,CustomerName,FeatureValue ' + @Columns + '
FROM
(
  select F.ItemId,F.ItemId as ItemCode,I.IPN,I.PartnerName,I.PartnerPart,c.CustomerName,t.FeatureName,FeatureValue  from #InputData Itm 
 inner join #ItemFeatures F on F.CustomerId=Itm.CustomerId  
 inner join #Items I on I.ItemID=F.ItemId
 inner join #FeatureType T on T.FeatureId=F.FeatureId 
 inner join #customers c on c.CustomerID=F.CustomerID 
) as PivotData
PIVOT
(
   COUNT(ItemId)
   FOR FeatureName IN (' + @Columns + ')
) AS PivotResult
ORDER BY CustomerName'

EXEC(@SQLs)

1 Ответ

0 голосов
/ 04 февраля 2020

Вы должны удалить неповторяющееся значение из оси:

SET @SQLs = 'SELECT ItemCode, IPN,PartnerName,CustomerName ' + @Columns + '
FROM
(
  select F.ItemId,F.ItemId as ItemCode,I.IPN,I.PartnerName,I.PartnerPart,c.CustomerName,t.FeatureName  from #InputData Itm 
 inner join #ItemFeatures F on F.CustomerId=Itm.CustomerId  
 inner join #Items I on I.ItemID=F.ItemId
 inner join #FeatureType T on T.FeatureId=F.FeatureId 
 inner join #customers c on c.CustomerID=F.CustomerID 
) as PivotData
PIVOT
(
   COUNT(ItemId)
   FOR FeatureName IN (' + @Columns + ')
) AS PivotResult
ORDER BY CustomerName'

FeatureValue имеет разные значения для каждой строки.

...