[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Join two tables and use PIVOT function

Posted on 2012-04-10
6
Medium Priority
?
264 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

825 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