Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
0
Answer
How to add feature value without prevent group data based on
ahmed elbarbary
5y
514
1
Reply
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)
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
Pivot table or operator
How to get month list in SQL Server?