TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed elbarbary
NA
1.6k
277.8k
How to add feature value without prevent group data based on
Feb 3 2020 9:33 PM
How to add feature value without prevent group data based on itemid pivot table?
I work on SQL server 2012
I make pivot table based on itemId
it work good but after add featurevalue data repeated
and not grouping
How to add Featurevalue without prevent repeated data on pivot table ?
desired result
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
What I have tried:
create
table
#InputData
(
CustomerID uniqueidentifier
)
insert
into
#InputData
values
(
'0ce19920-f0ca-433c-abb1-4e84d52b618b'
)
create
table
#customers
(
CustomerID uniqueidentifier,
CustomerName nvarchar(200)
)
insert
into
#customers
values
(
'0ce19920-f0ca-433c-abb1-4e84d52b618b'
,
'Michel'
),
(
'188b8053-18c0-4092-955e-962f54485e43'
,
'Jakson'
)
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'
),
(6,3,1,
'0ce19920-f0ca-433c-abb1-4e84d52b618b'
,
'88888'
),
(7,3,2,
'0ce19920-f0ca-433c-abb1-4e84d52b618b'
,
'99999'
)
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,F.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)
Reply
Answers (
0
)
Pivot table or operator
How to get month list in SQL Server?