June 5, 2018 at 7:24 am
Thanks so much for the useful insight and informationSelect Distinct
p.ID as IntProviderID
,p.LastName
,p.FirstName
,p.MiddleName
,ISNULL(REPLACE(CONVERT (CHAR(10),p.DateOfBirth,101),'/',''),'01011901') as DateOfBirth
,CASE p.Sex WHEN '' THEN 'U' ELSE p.Sex END as Sex
,a.AddressLine1 as PrimaryAddress1
,a.AddressLine2 as PrimaryAddress2
,a.City
,a.[State]
,a.zipcode
,a.PHONE
,a.FAX
,'' as SecondaryAddress1
,'' as SecondaryAddress2
,'' as SecondaryCity
,'' as SecondaryState
,'' as SecondaryZIP
,'' as SecondaryPhone
,'' as SecondaryFax
,ref.[DESCRIPTION] as PrimarySpecialty
,'' as SecondarySpecialty
,l.LicenseNumber as DEA
,REPLACE(CONVERT (CHAR(10),l.ExpirationDate ,101),'/','') as LicExpDate
,'' as StateLic
,'' as StateExpDate
,p.Upin
,p.Npi
,CASE p.Active WHEN 1 THEN 'Y' ELSE 'N' END as [Status]
,'' as InactiveDate
,p.DisplayDegrees_Short as ProvType
,'' as ProvTypeDesc
June 5, 2018 at 8:04 am
Here;s one way to handle it:SELECT DISTINCT
p.ID AS IntProviderID
,p.LastName
,p.FirstName
,p.MiddleName
,ISNULL(REPLACE(CONVERT(char(10), p.DateOfBirth, 101), '/', ''), '01011901') AS DateOfBirth
,CASE p.Sex WHEN '' THEN 'U' ELSE p.Sex END AS Sex
,a.AddressLine1 AS PrimaryAddress1
,a.AddressLine2 AS PrimaryAddress2
,a.City
,a.[State]
,a.zipcode
,a.PHONE
,a.FAX
,'' AS SecondaryAddress1
,'' AS SecondaryAddress2
,'' AS SecondaryCity
,'' AS SecondaryState
,'' AS SecondaryZIP
,'' AS SecondaryPhone
,'' AS SecondaryFax
,ref.[DESCRIPTION] AS PrimarySpecialty
,'' AS SecondarySpecialty
,l.LicenseNumber AS DEA
,REPLACE(CONVERT (CHAR(10),l.ExpirationDate ,101),'/','') AS LicExpDate
,'' AS StateLic
,'' AS StateExpDate
,p.Upin
,p.Npi
,CASE p.Active WHEN 1 THEN 'Y' ELSE 'N' END AS [Status]
,'' AS InactiveDate
,p.DisplayDegrees_Short AS ProvType
,'' AS ProvTypeDesc
,NULLIF(l.License_RTK, 'NPDBDEAxxx') AS StateLicense
FROM VisualCACTUS.Providers AS p
INNER JOIN VisualCACTUS.ProviderAddresses AS ak
ON p.Provider_K = ak.Provider_K
AND ak.Active = 1
INNER JOIN VisualCACTUS.ADDRESSES AS a
ON ak.Address_K = a.ADDRESS_K
AND a.ACTIVE = 1
INNER JOIN VisualCACTUS.ProviderSpecialties AS s
ON p.Provider_K = s.Provider_K
INNER JOIN VisualCACTUS.ProviderLicenses AS l
ON p.Provider_K = l.Provider_K
INNER JOIN VisualCACTUS.REFTABLE AS ref
ON s.Specialty_RTK = ref.REFTABLE_K
WHERE ak.AddressType_RTK = 'NPDBPRIMAR'
AND a.[state] NOT IN ('ID', 'MN')
AND l.License_RTK IN ('NPDBSTATEx', 'NPDBDEAxxx')
AND s.SpecialtyType_RTK = 'C4DJ0TXXIP'
AND p.Active = 1
ORDER BY LastName;
Note that I coded this on the assumption that only 2 license types are possible, and I just NULL out the value for StateLicense if the value for a Federal license type occurs. As it's the only value other than the State license type, this makes sense. If that changes, then you'll be better off with a CASE statement. The NULLIF function just nulls out the value only when it's 1st parameter is equal to the 2nd parameter, otherwise it provides the 1st parameter.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
June 5, 2018 at 8:07 am
Not real sure what you are asking for. Are ou wanting to set a field in your select statement for licensetype? You could use a CASE statement for that. Or are you talking about checking for this value in the where statement?
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
June 5, 2018 at 8:31 am
I am joining the License table based on Provider ID and the relationship is a one to many, however based on the license type (and for now there are two - State / Federal DEA) I only want to return the ONE record with the StateLic and DEA fields to return their respective data, if that makes sense. The NULLIF is returning the license type, but not the actual license number that I need. I am thinking a CASE statement might be better, right?
June 5, 2018 at 8:48 am
Still not clear to me what you are after.
If this is a one to many:INNER JOIN VisualCACTUS.ProviderLicenses AS l
ON p.Provider_K = l.Provider_K
Are you only wanting the 'state' license from this table? Is there a field on the table that you can use to filter to that one record? If so, include it in the join.
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
June 5, 2018 at 9:02 am
Okay, so if I have a list of 500 providers and let's take one, like Provider A. When I run the query, Provider A has TWO records because there is a license for State and a difference license for Federal. I want to merge those two into one record where the data from DEA will go into that DEALicense and State will go into the StateLic field. And if there isn't a record then that field would just be blank. For instance this needs to be just one record:
IntProviderID | LastName | FirstName | MiddleName | DateOfBirth | Sex | PrimaryAddress1 | PrimaryAddress2 | City | State | zipcode | PHONE | FAX | DEA | LicExpDate | StateLic | StateExpDate | Upin |
209 | A | Provider | 10241973 | U | 123 Main | Anytown | ST | 99999 | 5551234567 | NP9999999 | 6302020 | ||||||
209 | A | Provider | 10241973 | U | 123 Main | Anytown | ST | 99999 | 5551234567 | 123456789 | 1312019 |
June 5, 2018 at 9:50 am
Makes more sense now. So you want columns returned for the 'Fed' values and columns returned for the 'state' if they exist.
You could set up multiple joins to this table. See if something like this works.SELECT ...
lFed.DEA,
lState.StateLic
FROM ....
....
INNER JOIN VisualCACTUS.ProviderLicenses AS lFed
ON p.Provider_K = lFed.Provider_K
AND lFed.DEA IS NOT NULL -- if it is blank and not null use <> ''
INNER JOIN VisualCACTUS.ProviderLicenses AS lState
ON p.Provider_K = lState.Provider_K
AND lState.StateLic IS NOT NULL
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
June 5, 2018 at 10:31 am
Thanks so much! That worked. I have been working with SQL for nearly 20 years, but again, I don't author SQL scripts that much.
June 6, 2018 at 2:48 am
I suspect you'll actually want them to be LEFT joins, otherwise you'll eliminate rows that don't have both types of license, i.e.
SELECT ...
IsNull(IFed.DEA, ''), -- If you want to eliminate NULLs and just get empty strings
lState.StateLic
FROM ....
....
LEFT JOIN VisualCACTUS.ProviderLicenses AS lFed
ON p.Provider_K = lFed.Provider_K
AND lFed.DEA IS NOT NULL -- if it is blank and not null use <> ''
LEFT JOIN VisualCACTUS.ProviderLicenses AS lState
ON p.Provider_K = lState.Provider_K
AND lState.StateLic IS NOT NULL
[/code]
June 6, 2018 at 6:06 am
Thanks the for the information about LEFT join. I modified the query and it did populate with approximately 50 extra records.
June 6, 2018 at 6:27 am
Brad Allison - Tuesday, June 5, 2018 10:31 AMThanks so much! That worked. I have been working with SQL for nearly 20 years, but again, I don't author SQL scripts that much.
Given the data structures we get from Cactus, don't put down your SQL skills. It takes a lot of work to get the data you want from those tables.
June 6, 2018 at 6:36 am
Yeah, Symplr sent me the data table dictionary and it is pretty massive. We need some licensing reports for our Pharmacy that we are running using the Cactus DB. I just could not get my head around the proper joins, etc. and I thank those here that helped.
June 6, 2018 at 7:43 am
andycadley - Wednesday, June 6, 2018 2:48 AMI suspect you'll actually want them to be LEFT joins, otherwise you'll eliminate rows that don't have both types of license, i.e.SELECT ...
IsNull(IFed.DEA, ''), -- If you want to eliminate NULLs and just get empty strings
lState.StateLic
FROM ....
....
LEFT JOIN VisualCACTUS.ProviderLicenses AS lFed
ON p.Provider_K = lFed.Provider_K
AND lFed.DEA IS NOT NULL -- if it is blank and not null use <> ''
LEFT JOIN VisualCACTUS.ProviderLicenses AS lState
ON p.Provider_K = lState.Provider_K
AND lState.StateLic IS NOT NULL
[/code]
Thanks for catching that, trying to go to fast.
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply