Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Eliminating an Inner Query when using UNPIVOT

$
0
0

In the following query, I am attempting to UNPIVOT a number of different phone / fax numbers into a single column named 'PhoneNum'.

I haven't used UNPIVOT before, and I'm trying to understand a couple of things about it:

1. Why does the query run without error if I alias the outer select list with 'work'?
2. Can I write this in a way that I can eliminate the inner SELECT?

The following is the query I am working with:

SELECT DISTINCT
program.RecordNumber
,work.PhysicianEntityKey
,program.insurerKey
,work.ParFlag
,work.ProviderID1
,work.ProviderID2
,work.ProviderGroupID
,work.ProviderGroupName
,work.NetworkID
,work.EffectiveDate
,work.TerminationDate
,pn.PhoneNum
FROM 

SELECT distinct
RecordNumber, 
InsurerKey, 
PhysicianEntityKey,
ParFlag,
ProviderID1,
ProviderID2,
ProviderGroupID,
ProviderGroupName,
NetworkID,
EffectiveDate,
TerminationDate, 
PhysicalPhone, 
PhysicalFax, 
cast(BillingPhone as VARCHAR(10)) as BillingPhone, 
BillingFax 
FROM PMR_ETL.Provider_Work
) as  iq

UNPIVOT ( PhoneNum FOR PhoneUseType IN (iq.PhysicalPhone,
                                                iq.PhysicalFax,
                                                iq.BillingPhone,
                                                iq.BillingFax)) as work

LEFT JOIN Physician.Nexus pn
ON pn.NexusHashValue = CAST(work.PhoneNum as VARBINARY(40))
LEFT JOIN ProviderODS.Physician.Nexus ods
ON ods.NexusHashValue = CAST(work.PhoneNum as VARBINARY(40))
INNER JOIN PMR_ETL.Provider_Program_Work program
ON program.RecordNumber = work.RecordNumber
and program.InsurerKey = work.insurerKey

I hope I have communicated enough detail about my issue. Thank you for your help.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>