?
Solved

Pivot / Unpivot Brain Teaser

Posted on 2016-11-13
3
Medium Priority
?
44 Views
Last Modified: 2016-12-02
I'm looking to group records based on their values.
Below is an example of my data nd the output I am looking to achieve.
Any assistance would be greatly appreciated.

ID  LineID  F1     F2        F3
1   1       Joe    Thomas    Smith
1   1       David  Harry     Jones
1   2       Joe    Thomas    Smith
1   2       David  Hary      Jones
1   3       Joe    Thomas    Smith
1   4       Joe    Thomas    Smith
1   4       David  Harry     Jones
1   4       Carlos Jay       Smith
2   1       Jane   Janet     Garcia


Results I am trying to get.

ID  LineID  F1     F2        F3      R
1   1       Joe    Thomas    Smith   Joe~Thomas~Smith|David~Harry~Jones
1   1       David  Harry     Jones   Joe~Thomas~Smith|David~Harry~Jones
1   2       Joe    Thomas    Smith   Joe~Thomas~Smith|David~Harry~Jones
1   2       David  Hary      Jones   Joe~Thomas~Smith|David~Harry~Jones
1   3       Joe    Thomas    Smith   Joe~Thomas~Smith
1   4       Joe    Thomas    Smith   Joe~Thomas~Smith|David~Harry~Jones
1   4       David  Harry     Jones   Joe~Thomas~Smith|David~Harry~Jones
1   4       Carlos Jay       Smith   Joe~Thomas~Smith|David~Harry~Jones|Carlos~Jay~Smith
2   1       Jane   Janet     Garcia  Jane~Janet_Garcia

Open in new window

0
Comment
Question by:ScubeduFan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 1000 total points (awarded by participants)
ID: 41885412
Here it is, Please try

Table Creation

--

CREATE TABLE testPivot
(
	 ID     TINYINT 
	,LineID TINYINT 
	,F1     VARCHAR(25)
	,F2     VARCHAR(25)     
	,F3     VARCHAR(25)
)
GO

INSERT INTO testPivot VALUES
(1,   1,       'Joe'   , 'Thomas',    'Smith'),
(1,   1,       'David' , 'Harry'  ,   'Jones'),
(1,   2,       'Joe'   , 'Thomas'  ,  'Smith'),
(1,   2,       'David' , 'Hary'    ,  'Jones'),
(1,   3,       'Joe'   , 'Thomas'  ,  'Smith'),
(1,   4,       'Joe'   , 'Thomas'  ,  'Smith'),
(1,   4,       'David' , 'Harry'   ,  'Jones'),
(1,   4,       'Carlos', 'Jay'     ,  'Smith'),
(2,   1,       'Jane',   'Janet'   ,  'Garcia')
GO

--

Open in new window


Query - 1

--

SELECT m.ID, m.LineID , m.F1 , m.F2 , m.F3 ,  STUFF 
                ((
					SELECT '| ' + m2.N
					FROM (SELECT *, CONCAT(F1,'~',F2,'~',F3) N FROM testPivot) m2
					WHERE ( m.LineID = m2.LineID AND m.ID = m2.ID )
					FOR XML PATH('')
					) ,1,2,'') 
                R
FROM
(
	SELECT *, CONCAT(F1,'~',F2,'~',F3) N FROM testPivot
)m

--

Open in new window


Query 2

;WITH CTE AS
(
	SELECT *, F1 + '~' + F2 + '~' + F3 N FROM testPivot
)
SELECT m.ID, m.LineID , m.F1 , m.F2 , m.F3 ,  STUFF 
                ((
					SELECT '| ' + m2.N
					FROM CTE m2
					WHERE ( m.LineID = m2.LineID AND m.ID = m2.ID )
					FOR XML PATH('')
					) ,1,2,'') 
                R
FROM CTE m

Open in new window


Output

ID      LineID        F1      F2      F3                 R
1      1      Joe      Thomas      Smith      Joe~Thomas~Smith| David~Harry~Jones
1      1      David      Harry      Jones      Joe~Thomas~Smith| David~Harry~Jones
1      2      Joe      Thomas      Smith      Joe~Thomas~Smith| David~Hary~Jones
1      2      David      Hary      Jones      Joe~Thomas~Smith| David~Hary~Jones
1      3      Joe      Thomas      Smith      Joe~Thomas~Smith
1      4      Joe      Thomas      Smith      Joe~Thomas~Smith| David~Harry~Jones| Carlos~Jay~Smith
1      4      David      Harry      Jones      Joe~Thomas~Smith| David~Harry~Jones| Carlos~Jay~Smith
1      4      Carlos      Jay      Smith      Joe~Thomas~Smith| David~Harry~Jones| Carlos~Jay~Smith
2      1      Jane      Janet      Garcia      Jane~Janet~Garcia

Hope it helps !!
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1000 total points (awarded by participants)
ID: 41885456
For an image and code-heavy demo of what you're asking check out T-SQL:  Normalized data to a single comma delineated string and back
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41909942
Closing via Split.

Given what was asked.

Thank you !
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question