SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with Pivoting Data


Help with Pivoting Data

Author
Message
aobf
aobf
SSC-Enthusiastic
SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)

Group: General Forum Members
Points: 125 Visits: 41
Folks,
I'm looking for some suggestions. In my job, I work with value mapping on a daily basis, many times I am confronted with tables like below. When there is only 1 value mapping I needed to collapse, I was able to successfully use the Pivot function but most of the time, I will have multiple value mapping (1 row for each) that need to be "pivoted" so that I have 1 row per patient. I can't use pivot because its looking to aggregate the value. I need to be able to collapse the rows into 1 row. Please let me know if you can help me out. Let me know if you need more information.
TABLE1
PATIENT ID
PatientA 1234
PatientB 5678

TABLE2
ID CODE VALUE
1234 Code1 Value1
1234 Code2 Value2
1234 Code3 Value3
5678 Code1 Value1
5678 Code2 Value2
5678 Code3 Value3

NEEDED FORMAT - OUTPUT
PATIENT ID Code1 Code2 Code3
PatientA 1234 Value1 Value2 Value3
PatientB 5678 Value1 Value2 Value3
sgmunson
sgmunson
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98349 Visits: 7263
Try this "CROSS TAB" query:
WITH TABLE1 (PATIENT, ID) AS (

SELECT 'PatientA', 1234 UNION ALL
SELECT 'PatientB', 5678
),
TABLE2 (ID, CODE, [VALUE]) AS (

SELECT 1234, 'Code1', 'Value1' UNION ALL
SELECT 1234, 'Code2', 'Value2' UNION ALL
SELECT 1234, 'Code3', 'Value3' UNION ALL
SELECT 5678, 'Code1', 'Value1' UNION ALL
SELECT 5678, 'Code2', 'Value2' UNION ALL
SELECT 5678, 'Code3', 'Value3'
)
SELECT P.PATIENT, P.ID,
MAX(CASE C.CODE WHEN 'Code1' THEN [VALUE] ELSE NULL END) AS Code1,
MAX(CASE C.CODE WHEN 'Code2' THEN [VALUE] ELSE NULL END) AS Code2,
MAX(CASE C.CODE WHEN 'Code3' THEN [VALUE] ELSE NULL END) AS Code3
FROM TABLE1 AS P
INNER JOIN TABLE2 AS C
ON P.ID = C.ID
GROUP BY P.PATIENT, P.ID
ORDER BY P.ID;



Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search