Solved

Join two tables and use PIVOT function

Posted on 2012-04-10
6
250 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

920 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

19 Experts available now in Live!

Get 1:1 Help Now