Solved

Join two tables and use PIVOT function

Posted on 2012-04-10
6
253 Views
Last Modified: 2012-04-30
Experts,

This is the first time I've used the PIVOT function in SQL Server. The query below gives me the desired result.

SELECT PATIENTACCOUNT, FACILITYID, [1] AS PRINCPL_DX, [2] AS SEC_DX1, [3] AS SEC_DX2, [4] AS SEC_DX3, [5] AS SEC_DX4
      FROM
            (
            SELECT PATIENTACCOUNT, FACILITYID, ICD9DXSEQUENCE, ICD9DXCODE
            FROM T_ENCOUNTER_ICD9DX
            ) DXCODES
      PIVOT
            (
            MAX(ICD9DXCODE)
            FOR ICD9DXSEQUENCE IN ([1],[2],[3],[4],[5])
            ) AS PVT

I'm wondering if it's possible to join to another table to achieve the result in this example below.
I've been searching a while now and can't seem to find anything that gets me to where I want to go.

I want to join the T_ENCOUNTER_ICD9DX table to a T_ICD9DX_Codes table  ON
ICD9DXCODE column to get the Description of the Codes.

My desired result would be display the columns like ...

PATIENTACCOUNT, FACILITYID, [1] AS PRINCPL_DX, Princ_dx_description, [2] AS SEC_DX1, Sec_dx1_description,...

I've attached a simple create table /  insert values script.

Any help is appreciated.
pivot-setup-table-script.sql
0
Comment
Question by:jvoconnell
[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
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37829052
SELECT PATIENTACCOUNT, FACILITYID, [1] AS PRINCPL_DX, [2] AS SEC_DX1, [3] AS SEC_DX2, [4] AS SEC_DX3, [5] AS SEC_DX4,[6] as SEC_DX5
      FROM
            (
            SELECT PATIENTACCOUNT, FACILITYID, ICD9DXSEQUENCE, ICD9DXCODE,b.description
            FROM T_ENCOUNTER_ICD9DX a join T_ICD9DX_Codes b
             ON a.ICD9DXCODE = b.ICD9DXCODE 
            ) DXCODES 
      PIVOT
            (
            MAX(ICD9DXCODE)
            FOR ICD9DXSEQUENCE IN ([1],[2],[3],[4],[5],[6])
            ) AS PVT 

Open in new window


regards,
0
 
LVL 1

Author Comment

by:jvoconnell
ID: 37829603
Thank you for the input. This option results in the query returning multiple rows where as the original query returned one. I was hoping it was possible to keep the one row and have the descriptions be on the same line.  Sorry about that, I don't think I was clear. I apologize. For now, I'll just populate a table with the results from the query and then do a second pass and go back and get the descriptions.
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37829664
example of cte

http://msdn.microsoft.com/en-us/library/ms186243.aspx

There are too many examples you will find after googling...but i am giving simple one as below.

;with testcte(ID)
as
(select 1
union all
select ID+1 from testcte where ID < 5
)
select * from testcte

as above example will give a result of 1 to 4 and stop for 5.

thanks.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 14

Expert Comment

by:nishant joshi
ID: 37829709
sry for above solution
might other table having multiple records match so that it will goving more output...
0
 
LVL 1

Accepted Solution

by:
jvoconnell earned 0 total points
ID: 37893457
I wanted to close the loop on this question. From the suggestions, I couldn't get the desired output I wanted. We ended up using multiple queries instead of the PIVOT feature due to time constraints.  I do want to thank you for your time and assistance
0
 
LVL 1

Author Closing Comment

by:jvoconnell
ID: 37909680
Used multiple queries instead of PIVOT feature
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.

697 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