Solved

Pivot / Unpivot Brain Teaser

Posted on 2016-11-13
3
16 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
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 250 total points (awarded by participants)
Comment Utility
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 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points (awarded by participants)
Comment Utility
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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Closing via Split.

Given what was asked.

Thank you !
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now