I am trying to return all languages spoken by an individual in a single field to use in a SSRS report.
SELECT DISTINCT
-- #Selection Fields
Demo.FirstName
,Demo.LastName
,Demo.MiddleName
,Demo.NationalProviderID AS [NPI 1]
,Demo.PractitionerFormalTitle AS [License abv]
,Demo.ReceivedDate AS [Start Date]
,Demo.Notes AS [Subspecialties]
,Demo.SocialSecurityNumber
,Demo.GenderName
,Demo.BirthDate
,Demo.CompletedDate AS [Original Delegation Date]
,Demo.RenewalDate AS [Recred Due]
,Demo.CompletedDate AS [Last Recred Completed]
,CAQH.IDNumber AS [CAQH Number]
,TIN.TaxIDNumber AS [TIN]
,Location.PracticeNationalProviderID AS [NPI 2]
,Location.LineNumber1 AS [Location 1]
,Location.LineNumber2 AS [Location 2]
,Location.City AS [Location City]
,Location.[State] AS [Location State]
,Location.ZipCode AS [Location Zip]
,Location.TwentyFourHourCoverage [ADA Compliant]
,Location.PracticeLegalName
,Medicare.ProviderNumber [Medicare Number]
,Education.DegreeName
,Education.InstitutionName AS [School Name]
,Education.DateFrom AS [School Start Date]
,Education.DateTo AS [School End Date]
,SchoolAddress.LineNumber1 AS [School Line1]
,SchoolAddress.LineNumber2 AS [School Line2]
,SchoolAddress.City AS [School City]
,SchoolAddress.[STATE] AS [School State]
,SchoolAddress.ZipCode AS [School Zip]
,State.IssuingState AS [License State]
,State.LicenseNumber AS [State License Number]
,State.IssueDate AS [State Issue Date]
,State.ExpirationDate AS [State Expiration Date]
,State.ActiveStatePractice AS [Medicare Products y/n]
,DEA.LicenseNumber AS [DEA Number]
,DEA.IssueDate AS [DEA Issue Date]
,DEA.ExpirationDate AS [DEA Expiration]
,DEA.IssuingState AS [DEA State]
,CDS.LicenseNumber AS [CDS Number]
,CDS.IssueDate AS [CDS Issue Date]
,CDS.ExpirationDate AS [CDS Expiration]
,Specialties.TaxonomyCode
,Specialties.BoardStatusTypeName AS [Board Name]
,Specialties.BoardStatusTypeID
,Specialties.CertificationDate
,Specialties.ExpirationDate AS [Board Expiration]
,Specialties.CertificationNumber
,Email.EmailAddress
Currently I have this long Concat that brings together all the languages that are linked to a person.
,CONCAT (English.LanguageName, ASL.LanguageName,
Amharic.LanguageName, Swahili.LanguageName, Anang.LanguageName,
Arabic.LanguageName, Armenian.LanguageName,
Azarbaijani.LanguageName, Azeri.LanguageName,
Bengali.LanguageName, Bulgarian.LanguageName,
CanadianFrench.LanguageName, Cantonese.LanguageName,
Chaldean.LanguageName, Chinese.LanguageName, Creole.LanguageName,
Croatian.LanguageName, Czech.LanguageName, Danish.LanguageName,
Dari.LanguageName, Dutch.LanguageName, Efik.LanguageName,
Farsi.LanguageName, Filipino.LanguageName, Flemish.LanguageName,
French.LanguageName, German.LanguageName, Greek.LanguageName,
Gujarati.LanguageName, Haitian.LanguageName, Hakkai.LanguageName,
Hebrew.LanguageName, Hindi.LanguageName, Hindustani.LanguageName,
Llocano.LanguageName, Indian.LanguageName, Italian.LanguageName,
Japanese.LanguageName, Kannada.LanguageName,
Kiswahili.LanguageName, Konkani.LanguageName,
Korean.LanguageName, Kurdish.LanguageName, Laotian.LanguageName,
Latvian.LanguageName, Lithuanian.LanguageName,
Macedonian.LanguageName, Malayalam.LanguageName,
MandarinChinese.LanguageName, Marathi.LanguageName,
Marwari.LanguageName, Nigerian.LanguageName,
Norwegian.LanguageName, oriya.LanguageName, Pahari.LanguageName,
Pashto.LanguageName, Persian.LanguageName,
Philipino.LanguageName, Polish.LanguageName,
Portuguese.LanguageName, Punjabi.LanguageName,
Romanian.LanguageName, Russian.LanguageName,
Sanskrit.LanguageName, Serbian.LanguageName,
Sicilian.LanguageName, Sindhi.LanguageName,
Sinhalese.LanguageName, Slavic.LanguageName,
Spanish.LanguageName, Syriac.LanguageName, Tagalog.LanguageName,
Taiwanese.LanguageName, Tamil.LanguageName, Telugu.LanguageName,
Thai.LanguageName, Tigrinya.LanguageName, Turkish.LanguageName,
Ukrainian.LanguageName, Urdu.LanguageName,
Vietnamese.LanguageName, Yoruba.LanguageName,
Yugoslavian.LanguageName) AS [Languages]
-- #endSelcetion fields
FROM vwPractitionerDemographics Demo
LEFT OUTER JOIN vwPractitionerEmails Email
ON Email.PractitionerID = Demo.PractitionerID
AND Email.EmailTypeID = '1111197'
LEFT OUTER JOIN vwPractitionerEducation Education
ON Education.PractitionerID = Demo.PractitionerID
AND Education.Completed = 'Y'
LEFT OUTER JOIN vwInstitutionAddresses SchoolAddress
ON SchoolAddress.InstitutionID = Education.InstitutionID
LEFT OUTER JOIN vwPractitionerSpecialties Specialties
ON Specialties.PractitionerID = Demo.PractitionerID
AND Specialties.PrimarySpecialty = 'Y'
And Specialties.CertificationNumber Is Not null
LEFT OUTER JOIN vwPractitionerAlternateIDs CAQH
ON CAQH.ProviderID = Demo.PractitionerID
AND CAQH.AlternateIDTypeID = '1111033'
LEFT OUTER JOIN vwPractitionerProducts Plans
ON Plans.PractitionerID = Demo.PractitionerID
--Join all Licenses by Types
LEFT OUTER JOIN vwPractitionerLicenses State
ON Demo.PractitionerID = State.PractitionerID
AND state.LicenseTypeCode = 'State'
And state.Archived = 'N'
LEFT OUTER JOIN vwPractitionerLicenses DEA
ON Demo.PractitionerID = DEA.PractitionerID
AND DEA.LicenseTypeCode = 'DEA'
AND DEA.IssuingState = State.IssuingState
LEFT OUTER JOIN vwPractitionerLicenses CDS
ON Demo.PractitionerID = CDS.PractitionerID
AND CDS.LicenseTypeCode = 'CDS'
And CDS.IssuingState = State.IssuingState
--Locations linked to applicable licenses
LEFT OUTER JOIN vwPractitionerLocations Location
ON Plans.PractitionerID = Location.PractitionerID
AND Location.[State] = State.IssuingState
And Location.PracticeID = Plans.PracticeID
Left Outer Join vwPractitionerProducts Medicare
On Medicare.PractitionerID = Demo.PractitionerID
And Medicare.PracticeID = Location.PracticeID
And Medicare.ProviderNumber Is Not null
Left Outer Join vwPracticeLocations TIN
On TIN.PracticeID = Location.PracticeID
So there are currently all the following Joins to query the same table for possible languages that could be linked to a person.
--Joins or each language so each language can be queried
separately and placed in the same cell for all languages that are
present in the database
LEFT OUTER JOIN vwPractitionerLanguages English
ON English.PractitionerID = Demo.PractitionerID
AND English.LanguageID = '13272'
LEFT OUTER JOIN vwPractitionerLanguages Swahili
ON Swahili.PractitionerID = Demo.PractitionerID
AND Swahili.LanguageID = '35055'
LEFT OUTER JOIN vwPractitionerLanguages ASL
ON ASL.PractitionerID = Demo.PractitionerID
AND ASL.LanguageID = '35050'
LEFT OUTER JOIN vwPractitionerLanguages Amharic
ON Amharic.PractitionerID = Demo.PractitionerID
AND Amharic.LanguageID = '35001'
LEFT OUTER JOIN vwPractitionerLanguages Anang
ON Anang.PractitionerID = Demo.PractitionerID
AND Anang.LanguageID = '35002'
LEFT OUTER JOIN vwPractitionerLanguages Arabic
ON Arabic.PractitionerID = Demo.PractitionerID
AND Arabic.LanguageID = '13265'
LEFT OUTER JOIN vwPractitionerLanguages Armenian
ON Armenian.PractitionerID = Demo.PractitionerID
AND Armenian.LanguageID = '13266'
LEFT OUTER JOIN vwPractitionerLanguages Azarbaijani
ON Azarbaijani.PractitionerID = Demo.PractitionerID
AND Azarbaijani.LanguageID = '35004'
LEFT OUTER JOIN vwPractitionerLanguages Azeri
ON Azeri.PractitionerID = Demo.PractitionerID
AND Swahili.LanguageID = '1111802'
LEFT OUTER JOIN vwPractitionerLanguages Bengali
ON Bengali.PractitionerID = Demo.PractitionerID
AND Bengali.LanguageID = '35005'
LEFT OUTER JOIN vwPractitionerLanguages Bulgarian
ON Bulgarian.PractitionerID = Demo.PractitionerID
AND Bulgarian.LanguageID = '35006'
LEFT OUTER JOIN vwPractitionerLanguages CanadianFrench
ON CanadianFrench.PractitionerID = Demo.PractitionerID
AND CanadianFrench.LanguageID = '13267'
LEFT OUTER JOIN vwPractitionerLanguages Cantonese
ON Cantonese.PractitionerID = Demo.PractitionerID
AND Cantonese.LanguageID = '35007'
LEFT OUTER JOIN vwPractitionerLanguages Chaldean
ON Chaldean.PractitionerID = Demo.PractitionerID
AND Chaldean.LanguageID = '35008'
LEFT OUTER JOIN vwPractitionerLanguages Chinese
ON Chinese.PractitionerID = Demo.PractitionerID
AND Chinese.LanguageID = '13268'
LEFT OUTER JOIN vwPractitionerLanguages Creole
ON Creole.PractitionerID = Demo.PractitionerID
AND Creole.LanguageID = '35009'
LEFT OUTER JOIN vwPractitionerLanguages Croatian
ON Croatian.PractitionerID = Demo.PractitionerID
AND Croatian.LanguageID = '35010'
LEFT OUTER JOIN vwPractitionerLanguages Czech
ON Czech.PractitionerID = Demo.PractitionerID
AND Czech.LanguageID = '35011'
LEFT OUTER JOIN vwPractitionerLanguages Danish
ON Danish.PractitionerID = Demo.PractitionerID
AND Danish.LanguageID = '13270'
LEFT OUTER JOIN vwPractitionerLanguages Dari
ON Dari.PractitionerID = Demo.PractitionerID
AND Dari.LanguageID = '35012'
LEFT OUTER JOIN vwPractitionerLanguages Dutch
ON Dutch.PractitionerID = Demo.PractitionerID
AND Dutch.LanguageID = '13271'
LEFT OUTER JOIN vwPractitionerLanguages Efik
ON Efik.PractitionerID = Demo.PractitionerID
AND Efik.LanguageID = '35013'
LEFT OUTER JOIN vwPractitionerLanguages Farsi
ON Farsi.PractitionerID = Demo.PractitionerID
AND Farsi.LanguageID = '35014'
LEFT OUTER JOIN vwPractitionerLanguages Filipino
ON Filipino.PractitionerID = Demo.PractitionerID
AND Filipino.LanguageID = '35016'
LEFT OUTER JOIN vwPractitionerLanguages Flemish
ON Flemish.PractitionerID = Demo.PractitionerID
AND Flemish.LanguageID = '13273'
LEFT OUTER JOIN vwPractitionerLanguages French
ON French.PractitionerID = Demo.PractitionerID
AND French.LanguageID = '13274'
LEFT OUTER JOIN vwPractitionerLanguages German
ON German.PractitionerID = Demo.PractitionerID
AND German.LanguageID = '13275'
LEFT OUTER JOIN vwPractitionerLanguages Greek
ON Greek.PractitionerID = Demo.PractitionerID
AND Greek.LanguageID = '13276'
LEFT OUTER JOIN vwPractitionerLanguages Gujarati
ON Gujarati.PractitionerID = Demo.PractitionerID
AND Gujarati.LanguageID = '13273'
LEFT OUTER JOIN vwPractitionerLanguages Haitian
ON Haitian.PractitionerID = Demo.PractitionerID
AND Haitian.LanguageID = '1116065'
LEFT OUTER JOIN vwPractitionerLanguages Hakkai
ON Hakkai.PractitionerID = Demo.PractitionerID
AND Hakkai.LanguageID = '35018'
LEFT OUTER JOIN vwPractitionerLanguages Hebrew
ON Hebrew.PractitionerID = Demo.PractitionerID
AND Hebrew.LanguageID = '13277'
LEFT OUTER JOIN vwPractitionerLanguages Hindi
ON Hindi.PractitionerID = Demo.PractitionerID
AND Hindi.LanguageID = '35019'
LEFT OUTER JOIN vwPractitionerLanguages Hindustani
ON Hindustani.PractitionerID = Demo.PractitionerID
AND Hindustani.LanguageID = '13278'
LEFT OUTER JOIN vwPractitionerLanguages Llocano
ON Llocano.PractitionerID = Demo.PractitionerID
AND Llocano.LanguageID = '1117304'
LEFT OUTER JOIN vwPractitionerLanguages Indian
ON Indian.PractitionerID = Demo.PractitionerID
AND Indian.LanguageID = '35025'
LEFT OUTER JOIN vwPractitionerLanguages Italian
ON Italian.PractitionerID = Demo.PractitionerID
AND Italian.LanguageID = '13279'
LEFT OUTER JOIN vwPractitionerLanguages Japanese
ON Japanese.PractitionerID = Demo.PractitionerID
AND Japanese.LanguageID = '13280'
LEFT OUTER JOIN vwPractitionerLanguages Kannada
ON Kannada.PractitionerID = Demo.PractitionerID
AND Kannada.LanguageID = '35026'
LEFT OUTER JOIN vwPractitionerLanguages Kiswahili
ON Kiswahili.PractitionerID = Demo.PractitionerID
AND Kiswahili.LanguageID = '35027'
LEFT OUTER JOIN vwPractitionerLanguages Konkani
ON Konkani.PractitionerID = Demo.PractitionerID
AND Konkani.LanguageID = '35028'
LEFT OUTER JOIN vwPractitionerLanguages Korean
ON Korean.PractitionerID = Demo.PractitionerID
AND Korean.LanguageID = '35029'
LEFT OUTER JOIN vwPractitionerLanguages Kurdish
ON Kurdish.PractitionerID = Demo.PractitionerID
AND Kurdish.LanguageID = '35030'
LEFT OUTER JOIN vwPractitionerLanguages Laotian
ON Laotian.PractitionerID = Demo.PractitionerID
AND Laotian.LanguageID = '1116043'
LEFT OUTER JOIN vwPractitionerLanguages Latvian
ON Latvian.PractitionerID = Demo.PractitionerID
AND Latvian.LanguageID = '35031'
LEFT OUTER JOIN vwPractitionerLanguages Lithuanian
ON Lithuanian.PractitionerID = Demo.PractitionerID
AND Lithuanian.LanguageID = '35032'
LEFT OUTER JOIN vwPractitionerLanguages Macedonian
ON Macedonian.PractitionerID = Demo.PractitionerID
AND Macedonian.LanguageID = '35033'
LEFT OUTER JOIN vwPractitionerLanguages Malayalam
ON Malayalam.PractitionerID = Demo.PractitionerID
AND Malayalam.LanguageID = '35034'
LEFT OUTER JOIN vwPractitionerLanguages MandarinChinese
ON MandarinChinese.PractitionerID = Demo.PractitionerID
AND MandarinChinese.LanguageID = '35035'
LEFT OUTER JOIN vwPractitionerLanguages Marathi
ON Marathi.PractitionerID = Demo.PractitionerID
AND Marathi.LanguageID = '35036'
LEFT OUTER JOIN vwPractitionerLanguages Marwari
ON Marwari.PractitionerID = Demo.PractitionerID
AND Marwari.LanguageID = '35037'
LEFT OUTER JOIN vwPractitionerLanguages Nigerian
ON Nigerian.PractitionerID = Demo.PractitionerID
AND Nigerian.LanguageID = '1117126'
LEFT OUTER JOIN vwPractitionerLanguages Norwegian
ON Norwegian.PractitionerID = Demo.PractitionerID
AND Norwegian.LanguageID = '1116045'
LEFT OUTER JOIN vwPractitionerLanguages Oriya
ON Oriya.PractitionerID = Demo.PractitionerID
AND Oriya.LanguageID = '35038'
LEFT OUTER JOIN vwPractitionerLanguages Pahari
ON Pahari.PractitionerID = Demo.PractitionerID
AND Pahari.LanguageID = '35039'
LEFT OUTER JOIN vwPractitionerLanguages Pashto
ON Pashto.PractitionerID = Demo.PractitionerID
AND Pashto.LanguageID = '35041'
LEFT OUTER JOIN vwPractitionerLanguages Persian
ON Persian.PractitionerID = Demo.PractitionerID
AND Persian.LanguageID = '35042'
LEFT OUTER JOIN vwPractitionerLanguages Philipino
ON Philipino.PractitionerID = Demo.PractitionerID
AND Philipino.LanguageID = '35043'
LEFT OUTER JOIN vwPractitionerLanguages Polish
ON Polish.PractitionerID = Demo.PractitionerID
AND Polish.LanguageID = '13281'
LEFT OUTER JOIN vwPractitionerLanguages Portuguese
ON Portuguese.PractitionerID = Demo.PractitionerID
AND Portuguese.LanguageID = '13282'
LEFT OUTER JOIN vwPractitionerLanguages Punjabi
ON Punjabi.PractitionerID = Demo.PractitionerID
AND Punjabi.LanguageID = '35044'
LEFT OUTER JOIN vwPractitionerLanguages Romanian
ON Romanian.PractitionerID = Demo.PractitionerID
AND Romanian.LanguageID = '35045'
LEFT OUTER JOIN vwPractitionerLanguages Russian
ON Russian.PractitionerID = Demo.PractitionerID
AND Russian.LanguageID = '13283'
LEFT OUTER JOIN vwPractitionerLanguages Sanskrit
ON Sanskrit.PractitionerID = Demo.PractitionerID
AND Sanskrit.LanguageID = '35046'
LEFT OUTER JOIN vwPractitionerLanguages Serbian
ON Serbian.PractitionerID = Demo.PractitionerID
AND Serbian.LanguageID = '35048'
LEFT OUTER JOIN vwPractitionerLanguages Sicilian
ON Sicilian.PractitionerID = Demo.PractitionerID
AND Sicilian.LanguageID = '35049'
LEFT OUTER JOIN vwPractitionerLanguages Sindhi
ON Sindhi.PractitionerID = Demo.PractitionerID
AND Sindhi.LanguageID = '35051'
LEFT OUTER JOIN vwPractitionerLanguages Sinhalese
ON Sinhalese.PractitionerID = Demo.PractitionerID
AND Sinhalese.LanguageID = '35052'
LEFT OUTER JOIN vwPractitionerLanguages Slavic
ON Slavic.PractitionerID = Demo.PractitionerID
AND Slavic.LanguageID = '13284'
LEFT OUTER JOIN vwPractitionerLanguages Spanish
ON Spanish.PractitionerID = Demo.PractitionerID
AND Spanish.LanguageID = '13285'
LEFT OUTER JOIN vwPractitionerLanguages Syriac
ON Syriac.PractitionerID = Demo.PractitionerID
AND Syriac.LanguageID = '35056'
LEFT OUTER JOIN vwPractitionerLanguages Tagalog
ON Tagalog.PractitionerID = Demo.PractitionerID
AND Tagalog.LanguageID = '35057'
LEFT OUTER JOIN vwPractitionerLanguages Taiwanese
ON Taiwanese.PractitionerID = Demo.PractitionerID
AND Taiwanese.LanguageID = '35058'
LEFT OUTER JOIN vwPractitionerLanguages Tamil
ON Tamil.PractitionerID = Demo.PractitionerID
AND Tamil.LanguageID = '35059'
LEFT OUTER JOIN vwPractitionerLanguages Telugu
ON Telugu.PractitionerID = Demo.PractitionerID
AND Telugu.LanguageID = '35060'
LEFT OUTER JOIN vwPractitionerLanguages Thai
ON Thai.PractitionerID = Demo.PractitionerID
AND Thai.LanguageID = '35061'
LEFT OUTER JOIN vwPractitionerLanguages Tigrinya
ON Tigrinya.PractitionerID = Demo.PractitionerID
AND Tigrinya.LanguageID = '35062'
LEFT OUTER JOIN vwPractitionerLanguages Turkish
ON Turkish.PractitionerID = Demo.PractitionerID
AND Turkish.LanguageID = '35063'
LEFT OUTER JOIN vwPractitionerLanguages Ukrainian
ON Ukrainian.PractitionerID = Demo.PractitionerID
AND Ukrainian.LanguageID = '35064'
LEFT OUTER JOIN vwPractitionerLanguages Urdu
ON Urdu.PractitionerID = Demo.PractitionerID
AND Urdu.LanguageID = '35065'
LEFT OUTER JOIN vwPractitionerLanguages Vietnamese
ON Vietnamese.PractitionerID = Demo.PractitionerID
AND Vietnamese.LanguageID = '35066'
LEFT OUTER JOIN vwPractitionerLanguages Yoruba
ON Yoruba.PractitionerID = Demo.PractitionerID
AND Yoruba.LanguageID = '1116005'
LEFT OUTER JOIN vwPractitionerLanguages Yugoslavian
ON Yugoslavian.PractitionerID = Demo.PractitionerID
AND Yugoslavian.LanguageID = '35067'
Where
Demo.Archived = 'N'
And Location.Archived = 'N'
And Plans.Archived = 'N'
AND Demo.ApplicationStatusID NOT IN ('1111152', '1118448')
AND Demo.PractitionerTypeID NOT IN ('1112367', '1119027')
And Location.[State] In (#State)
This has over 200 joins and a very long CONCAT column. What is more efficient and would speed up the query?
My issue is I don't know which languages a person would have and I need them all to pull in the same column.
Using the STUFF() function, combined with a FOR XML sub-query, is a reasonably well-known method to achieve the result you're looking (that is, take column values from multiple rows and output as a single, concatenated value)
SELECT DISTINCT
-- #Selection Fields
Demo.FirstName
,Demo.LastName
-- all of your other fields in here, but I haven't included them in this example
,Email.EmailAddress
,
STUFF(
(SELECT ',' + v.LanguageName
FROM vwPractitionerLanuages v
WHERE v.PractitionerID = [Demo].PractitionerID
ORDER BY v.LanguageName
FOR XML PATH('')), 1, 1, '') AS [LanguagesSpoken]
FROM vwPractitionerDemographics Demo
LEFT OUTER JOIN vwPractitionerEmails Email
ON Email.PractitionerID = Demo.PractitionerID
AND Email.EmailTypeID = '1111197'
LEFT OUTER JOIN vwPractitionerEducation Education
ON Education.PractitionerID = Demo.PractitionerID
AND Education.Completed = 'Y'
LEFT OUTER JOIN vwInstitutionAddresses SchoolAddress
ON SchoolAddress.InstitutionID = Education.InstitutionID
LEFT OUTER JOIN vwPractitionerSpecialties Specialties
ON Specialties.PractitionerID = Demo.PractitionerID
AND Specialties.PrimarySpecialty = 'Y'
And Specialties.CertificationNumber Is Not null
LEFT OUTER JOIN vwPractitionerAlternateIDs CAQH
ON CAQH.ProviderID = Demo.PractitionerID
AND CAQH.AlternateIDTypeID = '1111033'
LEFT OUTER JOIN vwPractitionerProducts Plans
ON Plans.PractitionerID = Demo.PractitionerID
--Join all Licenses by Types
LEFT OUTER JOIN vwPractitionerLicenses State
ON Demo.PractitionerID = State.PractitionerID
AND state.LicenseTypeCode = 'State'
And state.Archived = 'N'
LEFT OUTER JOIN vwPractitionerLicenses DEA
ON Demo.PractitionerID = DEA.PractitionerID
AND DEA.LicenseTypeCode = 'DEA'
AND DEA.IssuingState = State.IssuingState
LEFT OUTER JOIN vwPractitionerLicenses CDS
ON Demo.PractitionerID = CDS.PractitionerID
AND CDS.LicenseTypeCode = 'CDS'
And CDS.IssuingState = State.IssuingState
--Locations linked to applicable licenses
LEFT OUTER JOIN vwPractitionerLocations Location
ON Plans.PractitionerID = Location.PractitionerID
AND Location.[State] = State.IssuingState
And Location.PracticeID = Plans.PracticeID
Left Outer Join vwPractitionerProducts Medicare
On Medicare.PractitionerID = Demo.PractitionerID
And Medicare.PracticeID = Location.PracticeID
And Medicare.ProviderNumber Is Not null
Left Outer Join vwPracticeLocations TIN
On TIN.PracticeID = Location.PracticeID
Where
Demo.Archived = 'N'
And Location.Archived = 'N'
And Plans.Archived = 'N'
AND Demo.ApplicationStatusID NOT IN ('1111152', '1118448')
AND Demo.PractitionerTypeID NOT IN ('1112367', '1119027')
And Location.[State] In (#State)
There is a SQL function named STRING_AGG() which achieves the same result, however I believe that function is only supported in SQL 2017 and above (you've tagged your question with SSRS-2012, so I'm making an assumption that your database engine version is also 2012)
To get a comma separated list by user:
SELECT PractitionerID, STRING_AGG(LanguageName, ', ') as LangList
FROM vwPractitionerLanguages
GROUP BY PractionerID
You could join to that and it would be fast. (I used Demo.*, but it is better not to use a wildcard.)
SELECT X.Lang, Demo.*
FROM Demo
LEFT JOIN (
SELECT PractitionerID, STRING_AGG(LanguageName, ', ') as LangList
FROM vwPractitionerLanguages
GROUP BY PractionerID
) X on X.PractitionerID = Demo.PractitionerID
Related
I have a long query which is basically consist of two sections as follows,
SELECT
cph.clientPollId,
cph.clientPollSeqNo,
cph.clientId,
cph.pollDate,
d.deviceId,
allPollData_A.HEALTHY,
batteryPollData_A.BATTERY_CHARGE_CYCLES,
batteryPollData_A.BATTERY_CURRENT,
batteryPollData_A.BATTERY_CURRENT_MAX,
batteryPollData_A.BATTERY_EFFECTIVE_SOC,
batteryPollData_A.BATTERY_EFFECTIVE_SOC_EXPLANATION,
batteryPollData_A.BATTERY_ENERGY,
batteryPollData_A.BATTERY_POWER,
batteryPollData_A.BATTERY_POWER_MAX,
batteryPollData_A.BATTERY_REAL_SOC,
batteryPollData_A.BATTERY_STORED_ENERGY,
batteryPollData_A.BATTERY_TEMP,
batteryPollData_A.BATTERY_VOLTAGE,
batteryPollData_A.BATTERY_VOLTAGE_MAX,
batteryPollData_A.BATTERY_VOLTAGE_MIN,
batteryPollData_A.BMS_VERSON,
batteryPollData_A.CELL_TEMPERATURE_MAX,
batteryPollData_A.CELL_TEMPERATURE_MIN,
batteryPollData_A.CELL_VOLTAGE_MAX,
batteryPollData_A.CELL_VOLTAGE_MIN,
.
.
.
FROM clientPollHeader_A cph
JOIN device d on cph.clientId = d.clientId
JOIN client c on d.clientId = c.clientId
JOIN latestClientPoll lcp on cph.clientPollSeqNo = lcp.clientPollSeqNo
LEFT OUTER JOIN allPollData_A on cph.clientPollSeqNo = allPollData_A.clientPollSeqNo
and d.deviceId = allPollData_A.deviceId
LEFT OUTER JOIN batteryPollData_A on cph.clientPollSeqNo = batteryPollData_A.clientPollSeqNo
and d.deviceId = batteryPollData_A.deviceId
LEFT OUTER JOIN deltaPollData_A on cph.clientPollSeqNo = deltaPollData_A.clientPollSeqNo
and d.deviceId = deltaPollData_A.deviceId
LEFT OUTER JOIN dessPollData_A on cph.clientPollSeqNo = dessPollData_A.clientPollSeqNo
and d.deviceId = dessPollData_A.deviceId
LEFT OUTER JOIN programPollData_A on cph.clientPollSeqNo = programPollData_A.clientPollSeqNo
and d.deviceId = programPollData_A.deviceId
LEFT OUTER JOIN emersonthermostat1PollData_A on cph.clientPollSeqNo = emersonthermostat1PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat1PollData_A.deviceId
LEFT OUTER JOIN emersonthermostat2PollData_A on cph.clientPollSeqNo = emersonthermostat2PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat2PollData_A.deviceId
LEFT OUTER JOIN emersonthermostat3PollData_A on cph.clientPollSeqNo = emersonthermostat3PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat3PollData_A.deviceId
LEFT OUTER JOIN emersonthermostat4PollData_A on cph.clientPollSeqNo = emersonthermostat4PollData_A.clientPollSeqNo
and d.deviceId = emersonthermostat4PollData_A.deviceId
LEFT OUTER JOIN thermostatPollData_A on cph.clientPollSeqNo = thermostatPollData_A.clientPollSeqNo
and d.deviceId = thermostatPollData_A.deviceId
LEFT OUTER JOIN inverterPollData_A on cph.clientPollSeqNo = inverterPollData_A.clientPollSeqNo
and d.deviceId = inverterPollData_A.deviceId
LEFT OUTER JOIN ioboardPollData_A on cph.clientPollSeqNo = ioboardPollData_A.clientPollSeqNo
and d.deviceId = ioboardPollData_A.deviceId
LEFT OUTER JOIN kokambatteryPollData_A on cph.clientPollSeqNo = kokambatteryPollData_A.clientPollSeqNo
and d.deviceId = kokambatteryPollData_A.deviceId
LEFT OUTER JOIN maggiePollData_A on cph.clientPollSeqNo = maggiePollData_A.clientPollSeqNo
and d.deviceId = maggiePollData_A.deviceId
LEFT OUTER JOIN mercedesbatteryPollData_A on cph.clientPollSeqNo = mercedesbatteryPollData_A.clientPollSeqNo
and d.deviceId = mercedesbatteryPollData_A.deviceId
LEFT OUTER JOIN omronPollData_A on cph.clientPollSeqNo = omronPollData_A.clientPollSeqNo
and d.deviceId = omronPollData_A.deviceId
LEFT OUTER JOIN daikinacPollData_A on cph.clientPollSeqNo = daikinacPollData_A.clientPollSeqNo
and d.deviceId = daikinacPollData_A.deviceId
LEFT OUTER JOIN outbackradianPollData_A on cph.clientPollSeqNo = outbackradianPollData_A.clientPollSeqNo
and d.deviceId = outbackradianPollData_A.deviceId
LEFT OUTER JOIN pvgenPollData_A on cph.clientPollSeqNo = pvgenPollData_A.clientPollSeqNo
and d.deviceId = pvgenPollData_A.deviceId
LEFT OUTER JOIN safeplugPollData_A on cph.clientPollSeqNo = safeplugPollData_A.clientPollSeqNo
and d.deviceId = safeplugPollData_A.deviceId
LEFT OUTER JOIN schneiderinverterPollData_A on cph.clientPollSeqNo = schneiderinverterPollData_A.clientPollSeqNo
and d.deviceId = schneiderinverterPollData_A.deviceId
LEFT OUTER JOIN solarchargecontrollerPollData_A on cph.clientPollSeqNo = solarchargecontrollerPollData_A.clientPollSeqNo
and d.deviceId = solarchargecontrollerPollData_A.deviceId
LEFT OUTER JOIN owonPollData_A on cph.clientPollSeqNo = owonPollData_A.clientPollSeqNo
and d.deviceId = owonPollData_A.deviceId
LEFT OUTER JOIN ecobeePollData_A on cph.clientPollSeqNo = ecobeePollData_A.clientPollSeqNo
and d.deviceId = ecobeePollData_A.deviceId
LEFT OUTER JOIN lgPollData_A on cph.clientPollSeqNo = lgPollData_A.clientPollSeqNo
and d.deviceId = lgPollData_A.deviceId
LEFT OUTER JOIN eatonemcb1PollData_A on cph.clientPollSeqNo = eatonemcb1PollData_A.clientPollSeqNo
and d.deviceId = eatonemcb1PollData_A.deviceId
LEFT OUTER JOIN eyedroPollData_A on cph.clientPollSeqNo = eyedroPollData_A.clientPollSeqNo
and d.deviceId = eyedroPollData_A.deviceId
LEFT OUTER JOIN iflowPollData_A on cph.clientPollSeqNo = iflowPollData_A.clientPollSeqNo
and d.deviceId = iflowPollData_A.deviceId
WHERE
(
lcp.clientId IN
(
'01886fc9-3442-473a-85fa-3325f2e6a66d',
'94711a81-76eb-4865-9fc9-95c622071c24'
)
)
union
SELECT
cph.clientPollId,
cph.clientPollSeqNo,
cph.clientId,
cph.pollDate,
d.deviceId,
allPollData_B.HEALTHY,
batteryPollData_B.BATTERY_CHARGE_CYCLES,
batteryPollData_B.BATTERY_CURRENT,
batteryPollData_B.BATTERY_CURRENT_MAX,
batteryPollData_B.BATTERY_EFFECTIVE_SOC,
batteryPollData_B.BATTERY_EFFECTIVE_SOC_EXPLANATION,
batteryPollData_B.BATTERY_ENERGY,
batteryPollData_B.BATTERY_POWER,
batteryPollData_B.BATTERY_POWER_MAX,
batteryPollData_B.BATTERY_REAL_SOC,
batteryPollData_B.BATTERY_STORED_ENERGY,
batteryPollData_B.BATTERY_TEMP,
batteryPollData_B.BATTERY_VOLTAGE,
batteryPollData_B.BATTERY_VOLTAGE_MAX,
batteryPollData_B.BATTERY_VOLTAGE_MIN,
batteryPollData_B.BMS_VERSON,
batteryPollData_B.CELL_TEMPERATURE_MAX,
batteryPollData_B.CELL_TEMPERATURE_MIN,
batteryPollData_B.CELL_VOLTAGE_MAX,
batteryPollData_B.CELL_VOLTAGE_MIN,
.
.
.
FROM clientPollHeader_B cph
JOIN device d on cph.clientId = d.clientId
JOIN client c on d.clientId = c.clientId
JOIN latestClientPoll lcp on cph.clientPollSeqNo = lcp.clientPollSeqNo
LEFT OUTER JOIN allPollData_B on cph.clientPollSeqNo = allPollData_B.clientPollSeqNo
and d.deviceId = allPollData_B.deviceId
LEFT OUTER JOIN batteryPollData_B on cph.clientPollSeqNo = batteryPollData_B.clientPollSeqNo
and d.deviceId = batteryPollData_B.deviceId
LEFT OUTER JOIN deltaPollData_B on cph.clientPollSeqNo = deltaPollData_B.clientPollSeqNo
and d.deviceId = deltaPollData_B.deviceId
LEFT OUTER JOIN dessPollData_B on cph.clientPollSeqNo = dessPollData_B.clientPollSeqNo
and d.deviceId = dessPollData_B.deviceId
LEFT OUTER JOIN programPollData_B on cph.clientPollSeqNo = programPollData_B.clientPollSeqNo
and d.deviceId = programPollData_B.deviceId
LEFT OUTER JOIN emersonthermostat1PollData_B on cph.clientPollSeqNo = emersonthermostat1PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat1PollData_B.deviceId
LEFT OUTER JOIN emersonthermostat2PollData_B on cph.clientPollSeqNo = emersonthermostat2PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat2PollData_B.deviceId
LEFT OUTER JOIN emersonthermostat3PollData_B on cph.clientPollSeqNo = emersonthermostat3PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat3PollData_B.deviceId
LEFT OUTER JOIN emersonthermostat4PollData_B on cph.clientPollSeqNo = emersonthermostat4PollData_B.clientPollSeqNo
and d.deviceId = emersonthermostat4PollData_B.deviceId
LEFT OUTER JOIN thermostatPollData_B on cph.clientPollSeqNo = thermostatPollData_B.clientPollSeqNo
and d.deviceId = thermostatPollData_B.deviceId
LEFT OUTER JOIN inverterPollData_B on cph.clientPollSeqNo = inverterPollData_B.clientPollSeqNo
and d.deviceId = inverterPollData_B.deviceId
LEFT OUTER JOIN ioboardPollData_B on cph.clientPollSeqNo = ioboardPollData_B.clientPollSeqNo
and d.deviceId = ioboardPollData_B.deviceId
LEFT OUTER JOIN kokambatteryPollData_B on cph.clientPollSeqNo = kokambatteryPollData_B.clientPollSeqNo
and d.deviceId = kokambatteryPollData_B.deviceId
LEFT OUTER JOIN maggiePollData_B on cph.clientPollSeqNo = maggiePollData_B.clientPollSeqNo
and d.deviceId = maggiePollData_B.deviceId
LEFT OUTER JOIN mercedesbatteryPollData_B on cph.clientPollSeqNo = mercedesbatteryPollData_B.clientPollSeqNo
and d.deviceId = mercedesbatteryPollData_B.deviceId
LEFT OUTER JOIN omronPollData_B on cph.clientPollSeqNo = omronPollData_B.clientPollSeqNo
and d.deviceId = omronPollData_B.deviceId
LEFT OUTER JOIN daikinacPollData_B on cph.clientPollSeqNo = daikinacPollData_B.clientPollSeqNo
and d.deviceId = daikinacPollData_B.deviceId
LEFT OUTER JOIN outbackradianPollData_B on cph.clientPollSeqNo = outbackradianPollData_B.clientPollSeqNo
and d.deviceId = outbackradianPollData_B.deviceId
LEFT OUTER JOIN pvgenPollData_B on cph.clientPollSeqNo = pvgenPollData_B.clientPollSeqNo
and d.deviceId = pvgenPollData_B.deviceId
LEFT OUTER JOIN safeplugPollData_B on cph.clientPollSeqNo = safeplugPollData_B.clientPollSeqNo
and d.deviceId = safeplugPollData_B.deviceId
LEFT OUTER JOIN schneiderinverterPollData_B on cph.clientPollSeqNo = schneiderinverterPollData_B.clientPollSeqNo
and d.deviceId = schneiderinverterPollData_B.deviceId
LEFT OUTER JOIN solarchargecontrollerPollData_B on cph.clientPollSeqNo = solarchargecontrollerPollData_B.clientPollSeqNo
and d.deviceId = solarchargecontrollerPollData_B.deviceId
LEFT OUTER JOIN owonPollData_B on cph.clientPollSeqNo = owonPollData_B.clientPollSeqNo
and d.deviceId = owonPollData_B.deviceId
LEFT OUTER JOIN ecobeePollData_B on cph.clientPollSeqNo = ecobeePollData_B.clientPollSeqNo
and d.deviceId = ecobeePollData_B.deviceId
LEFT OUTER JOIN lgPollData_B on cph.clientPollSeqNo = lgPollData_B.clientPollSeqNo
and d.deviceId = lgPollData_B.deviceId
LEFT OUTER JOIN eatonemcb1PollData_B on cph.clientPollSeqNo = eatonemcb1PollData_B.clientPollSeqNo
and d.deviceId = eatonemcb1PollData_B.deviceId
LEFT OUTER JOIN eyedroPollData_B on cph.clientPollSeqNo = eyedroPollData_B.clientPollSeqNo
and d.deviceId = eyedroPollData_B.deviceId
LEFT OUTER JOIN iflowPollData_B on cph.clientPollSeqNo = iflowPollData_B.clientPollSeqNo
and d.deviceId = iflowPollData_B.deviceId
WHERE
(
lcp.clientId IN
(
'01886fc9-3442-473a-85fa-3325f2e6a66d',
'94711a81-76eb-4865-9fc9-95c622071c24'
)
)
the thing is the _A tables have value but _B tables are all empty (In this case they might get values). So the first part before the union which is for _A takes less than a second to run however the second part for_B tables (empty tables) takes 31 seconds. So my question is does outer join on empty tables create a bottleneck or what? Any help appreciated.
I'm trying to return a distinct list of patients given certain criteria. I'm using an inner join to look at all patient records that meet a certain criteria and then look at the MAX service date, and further the MAX charge amount for that day, to force a unique record. Below is an abbreviated example showing that a patient may have had 4 charges on the same service date.
I want to pull the MAX charge on THAT day to get a distinct record match. However, my current code is concatenating the MAX service date with a MAX charge from ANY found service date, creating a hypothetical record such as the following from the above example:
10/3/2016 9/30/2016 Sebert, Stephen L MD 76598 CHG 238
This record does not exist in the database so I'm losing the count and not returning a match to get the patient demographic information. I hope this makes sense. Here's my code. I'm using an inner joing to do it. Please let me know if there is a better way. Do I need to do a second nested inner join to accomplish this?
SELECT PAT.V51_PRSNPERSONNUMBER AS PSNNMBR,
(PAT.V51_PRSNFIRSTNAME + ' ' + PAT.V51_PRSNLASTNAME) AS NAME,
ADDR.V8_PERSONHOMEADDRLINE1 AS ADDRESS,
ADDR.V8_PERSONHOMEADDRLINE2 AS ADDRESS2,
ADDR.V8_PERSONHOMEADDRCITY AS CITY,
ADDR.V8_PERSONHOMEADDRSTATE AS STATE,
ADDR.V8_PERSONHOMEADDRZIP AS ZIP,
ADDR.V8_PERSONHOMEPHONENUMBER AS PHONE,
ADDR.V8_PERSONMOBILEPHONENUMBER AS CELLPHONE,
PAT.V51_PRSNEMAIL AS EMAIL,
'LOCASCIO REQUEST' AS GROUPS
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
INNER JOIN (
SELECT CP.DNCP_PATIENTID AS PSNNMBR,
MAX(CP.DNCP_FROMSERVICEDATE) AS SVCDT,
MAX(CP.DNCP_AMOUNT_CHARGE) AS CHGAMT
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
WHERE CP.OFFICE_ID = 'EDQV'
AND CP.DNCP_ACTIVITYTYPE = 'CHG'
AND SUBSTRING(DEP.V4_DPTNAME,1,5) IN ('PRIMA','WOMEN','URGIC')
AND (DATEDIFF(DAY,CP.DNCP_POSTDATE,GETDATE())) < 730
GROUP BY CP.DNCP_PATIENTID
) AS MAXSVCDATE
ON (CP.DNCP_PATIENTID = MAXSVCDATE.PSNNMBR AND CP.DNCP_FROMSERVICEDATE = MAXSVCDATE.SVCDT AND CP.DNCP_AMOUNT_CHARGE = MAXSVCDATE.CHGAMT)
WHERE CP.OFFICE_ID = 'EDQV'
The intended result would be the top record in the picture above with the charge of 217. Thank you so much for any help you can provide. Thanks!
you have to group not only by patient no on your subquery, but also by DNCP_FROMSERVICEDATE so the only max you would have is the DNCP_AMOUNT_CHARGE so you get a list of patients by day and the max charge for THAT day, then you have to filter that somehow because you're gonna get multiples per patient, maybe more groupings on the main query or a date filter or just MAX(DNCP_AMOUNT_CHARGE) on the main query and that's the only column you pull from the subquery.
hope it makes sense.
Hoping you can help.
We have an app that displays a grid. We can add custom fields to the grid using subqueries with which I am struggling. The main grid query looks like this.
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
The subquery is to basically provide a column that looks at the sales order and returns a count of row within that order. When I do this I get the following:
There was a problem retrieving the data:
subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, < etc
or when the subquery is used as an expression
Below is the query complete with subquery. If anyone can help that would be amazing
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
GROUP BY order_header.oh_order_number)
AS CustomLineCount
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
Try the below query, it may helps you
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
and order_header.oh_order_number=ordhead.oh_order_number)
AS CustomLineCount
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header ordhead
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
Change your sub query to this:
(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
AND order_header.oh_order_number = OUTER_QUERY_TABLE.SAME_FIELD )
I've added this line:
AND order_header.oh_order_number = OUTER_QUERY_TABLE.SAME_FIELD
You have to make this query correlated , so add the condition to match the record to the outer query . I didn't know which field it is.
You could also optimize your WHERE and make it more readable with IN() :
WHERE wo_pwos_id = 1
AND cd_ow_account IN('NOTHS','EBAY','4008','TRUSCA')
AND (wo_required_datetime <= '2016-12-24 23:59:59' OR wo_required_datetime IS NULL)
AND wo_wos_id <> 4
AND wo_kit = 0
This question already has answers here:
SQL NOT IN not working
(4 answers)
Closed 6 years ago.
First, here's my code:
SELECT --PAT.V51_PRSNPERSONNUMBER AS PSNNMBR,
(PAT.V51_PRSNFIRSTNAME + ' ' + PAT.V51_PRSNLASTNAME) AS NAME,
--ADDR.V8_PERSONHOMEADDRLINE1 AS ADDRESS,
--ADDR.V8_PERSONHOMEADDRLINE2 AS ADDRESS2,
--ADDR.V8_PERSONHOMEADDRCITY AS CITY,
--ADDR.V8_PERSONHOMEADDRSTATE AS STATE,
--ADDR.V8_PERSONHOMEADDRZIP AS ZIP,
--PAT.V51_PRSNDOB AS DOB,
--PAT.V51_PRSNSEX AS SEX,
ADDR.V8_PERSONHOMEPHONENUMBER AS PHONE,
ADDR.V8_PERSONMOBILEPHONENUMBER AS CELLPHONE,
--CV.V9_DNPLANSORTNAME AS INSPLAN,
--CV.V9_DNCARRIERNAME AS INSCARRIER,
PAT.V51_PRSNEMAIL AS EMAIL,
'RSQ TEST' AS GROUPS
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
INNER JOIN (
SELECT CP.DNCP_PATIENTID AS PSNNMBR,
MAX(CP.DNCP_FROMSERVICEDATE) AS SVCDT,
MAX(CP.DNCP_AMOUNT_CHARGE) AS CHGAMT
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
WHERE CP.OFFICE_ID = 'EDQV'
AND CP.DNCP_ACTIVITYTYPE = 'CHG'
--AND (CP.DNCP_PROCEDURECODE LIKE '99490' OR CP.DNCP_PROCEDURECODE LIKE 'CCM')
AND PAT.V51_PRSNSEX = 'F'
AND (DATEDIFF(DAY,PAT.V51_PRSNDOB,GETDATE())/365) >= 65
AND SUBSTRING(DEP.V4_DPTNAME,1,5) IN ('PRIMA','WOMEN','URGIC')
AND (DATEDIFF(DAY,CP.DNCP_POSTDATE,GETDATE())) < 720
--FOLLOWING PROVIDES NON DIABETICS
AND (DX.PROCEVENTDIAG1CODE NOT IN ('E11.65', 'E11.9', 'E10.9', 'E13.8', 'E10.39', 'E11.00', 'E11.69', 'E11.319', 'E11.42', 'E11.359', 'E11.329', 'E13.9', 'E10.8', 'E11.59', 'E11.39', 'H35.00') OR
DX.PROCEVENTDIAG2CODE NOT IN ('E11.65', 'E11.9', 'E10.9', 'E13.8', 'E10.39', 'E11.00', 'E11.69', 'E11.319', 'E11.42', 'E11.359', 'E11.329', 'E13.9', 'E10.8', 'E11.59', 'E11.39', 'H35.00') OR
DX.PROCEVENTDIAG3CODE NOT IN ('E11.65', 'E11.9', 'E10.9', 'E13.8', 'E10.39', 'E11.00', 'E11.69', 'E11.319', 'E11.42', 'E11.359', 'E11.329', 'E13.9', 'E10.8', 'E11.59', 'E11.39', 'H35.00') OR
DX.PROCEVENTDIAG4CODE NOT IN ('E11.65', 'E11.9', 'E10.9', 'E13.8', 'E10.39', 'E11.00', 'E11.69', 'E11.319', 'E11.42', 'E11.359', 'E11.329', 'E13.9', 'E10.8', 'E11.59', 'E11.39', 'H35.00'))
GROUP BY CP.DNCP_PATIENTID
) AS MAXSVCDATE
ON (CP.DNCP_PATIENTID = MAXSVCDATE.PSNNMBR AND CP.DNCP_FROMSERVICEDATE = MAXSVCDATE.SVCDT AND CP.DNCP_AMOUNT_CHARGE = MAXSVCDATE.CHGAMT)
WHERE CP.OFFICE_ID = 'EDQV'
AND PAT.V51_PRSNPERSONNUMBER NOT IN (
SELECT PAT.V51_PRSNPERSONNUMBER
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
WHERE CP.OFFICE_ID = 'EDQV'
AND CP.DNCP_ACTIVITYTYPE = 'CHG'
AND BILL.V58_PROVSORTNAME LIKE '%LOCASCIO%'
AND (DATEDIFF(DAY,CP.DNCP_POSTDATE,GETDATE())) < 720
)
If I run the statement and exclude everything after the main WHERE CP.OFFICE_ID = 'EDQV', it runs great. If I include everything and change it to an IN statement rather than a NOT IN statement, it runs great. I've run independent queries and pulled them into Excel to do it manually so I know what numbers of records I should be returning.
If I exclude the NOT IN statement all together, I get 3,315 records. If I make it an IN statement, I get 283, which is accurate and verified in Excel. Therefore, if I change it to a NOT IN statement, I should get 3,032 records. However, I get zero! Am I missing something stupid here?
If it helps, the point of this code is to return a contact list for all female patients over the age of 65 who are not diabetic and have been seen in primary care, womens' care, or urgent care in the last two years, who also have NOT been seen by Dr. Locascio in the last two years. Need it to only return one line per person as well.
My guess is that this is a case where PAT.V51_PRSNPERSONNUMBER can be null, most likely on the left of the NOT IN, but perhaps in the subselect too. In a predicate the expression NULL = NULL has the value UNKNOWN (as opposed to TRUE or FALSE). This has the effect of excluding any rows from the result. Since (NULL != NULL) also has the UNKNOWN value, the same rows are excluded either way. The same is true for the NOT IN predicate. If the above column is NULL, it won't match a NULL value returned in the subselect either with the IN or with the NOT IN operator. (This is typically not intuitive if you haven't experienced it before)
As to why PAT.V51_PRSNPERSONNUMBER is null, it may be in the join condition used to join in the PAT aliased instance of IVERSIONPAT view or table is returning no match -- for the left join it measn all PAT columns will be NULL. The first step is to run the subselect on its own and see if the above column is in fact returning NULL for some rows-- likely the full 3032 missing rows and go from there.
I've used this site for research for years but can't figure this one out from my research. Getting the following error:
Conversion failed when converting the varchar value '38221.0' to data type int.
This '38221.0' correlates to the 'CP.DNCP_PATIENTID' from the below code:
SELECT PAT.V51_PRSNPERSONNUMBER AS PSNNMBR,
PAT.V51_PRSNFIRSTNAME AS FIRSTNAME,
PAT.V51_PRSNMIDDLENAME AS MIDDLENAME,
PAT.V51_PRSNLASTNAME AS LASTNAME,
ADDR.V8_PERSONHOMEADDRLINE1 AS ADDRESS,
ADDR.V8_PERSONHOMEADDRLINE2 AS ADDRESS2,
ADDR.V8_PERSONHOMEADDRCITY AS CITY,
ADDR.V8_PERSONHOMEADDRSTATE AS STATE,
ADDR.V8_PERSONHOMEADDRZIP AS ZIP,
PAT.V51_PRSNDOB AS DOB,
PAT.V51_PRSNSEX AS SEX,
PAT.V51_PRSNSSN AS SSN,
ADDR.V8_PERSONHOMEPHONENUMBER AS HOMEPHONE,
ADDR.V8_PERSONMOBILEPHONENUMBER AS MOBILEPHONE,
CV.V9_DNPLANSORTNAME AS INSPLAN,
CV.V9_DNCARRIERNAME AS INSCARRIER,
COORD.PCCUSERNAME AS CARECOORDINATOR
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
LEFT OUTER JOIN IPATIENT IPAT
ON (CP.OFFICE_ID = IPAT.OFFICE_ID AND CP.DNCP_PATIENTID = IPAT.PATDNPERSONNUMBER)
LEFT OUTER JOIN I_VPATCARECOORD COORD
ON (IPAT.OFFICE_ID = COORD.OFFICE_ID AND IPAT.PATCARECOORDINATORID = COORD.PCCMMUSERSID)
INNER JOIN (
SELECT CP.DNCP_PATIENTID AS PSNNMBR,
MAX(CP.DNCP_FROMSERVICEDATE) AS SVCDT
--MIN(CP.DNCP_AMOUNT_CHARGE) AS CHGAMT
FROM I_DNCHGSPMTS CP
LEFT OUTER JOIN IPROCEDURE PR
ON (CP.DNCP_PROCEDURECODE = PR.PRPROCEDURECODE AND CP.OFFICE_ID = PR.OFFICE_ID)
LEFT OUTER JOIN I_VSTAFFCHGBILL STFBILL
ON (CP.OFFICE_ID = STFBILL.OFFICE_ID AND CP.DNCP_PRACTICEID = STFBILL.V69_STPRACTICEID AND
CP.DNCP_STAFFID_CREDITED = STFBILL.V69_STSTAFFID)
LEFT OUTER JOIN I_VPROVCHGBILL BILL
ON (STFBILL.V69_STSTAFFID = BILL.V58_PROVPROVIDERID AND STFBILL.OFFICE_ID = BILL.OFFICE_ID)
LEFT OUTER JOIN I_DNCHGCOVERAGE CHGCV
ON (CP.OFFICE_ID = CHGCV.OFFICE_ID AND CP.DNCP_CHARGESID = CHGCV.TPSCHARGESID)
LEFT OUTER JOIN I_VDNPLANCHGCV1 CV
ON (CHGCV.CHGCVR1PLANID = CV.V9_DNPLANID AND CHGCV.OFFICE_ID = CV.OFFICE_ID)
LEFT OUTER JOIN I_VDEPTCHG DEP
ON (CP.DNCP_DEPARTMENTCODE = DEP.V4_DPTDEPARTMENTCODE AND CP.OFFICE_ID = DEP.OFFICE_ID)
LEFT OUTER JOIN I_VDNADDRSPAT ADDR
ON (CP.OFFICE_ID = ADDR.OFFICE_ID AND CP.DNCP_PATIENTID = ADDR.V8_TPSPERSONID)
LEFT OUTER JOIN I_VPERSONPAT PAT
ON (CP.OFFICE_ID = PAT.OFFICE_ID AND CP.DNCP_PATIENTID = PAT.V51_PRSNPERSONID)
LEFT OUTER JOIN IACCOUNT ACC
ON (CP.OFFICE_ID = ACC.OFFICE_ID AND CP.DNCP_ACCOUNTSID = ACC.ACCACCOUNTSID)
LEFT OUTER JOIN IPAYMENT PMT
ON (CP.OFFICE_ID = PMT.OFFICE_ID AND CP.DNCP_PAYMENTSID = PMT.PMTPAYMENTSID)
LEFT OUTER JOIN I_VLUPROCCLASS CL
ON (PR.OFFICE_ID = CL.OFFICE_ID AND PR.PRCLASS = CL.V85_LUCLOOKUPCODE)
LEFT OUTER JOIN I_VSVCCNTRPROC SVC
ON (CP.OFFICE_ID = SVC.OFFICE_ID AND CP.DNCP_SERVICECENTERID = SVC.V75_SVCSERVICECENTERID)
LEFT OUTER JOIN I_DNDXPROCEVENT DX
ON (CP.OFFICE_ID = DX.OFFICE_ID AND CP.DNCP_PROCEDUREEVENTSID = DX.TPSPROCEDUREEVENTSID)
WHERE CP.OFFICE_ID = 'EDQV'
AND CP.DNCP_ACTIVITYTYPE = 'CHG'
AND (CP.DNCP_PROCEDURECODE LIKE '99490' OR CP.DNCP_PROCEDURECODE LIKE 'CCM')
AND (DATEDIFF(DAY,CP.DNCP_POSTDATE,GETDATE())) < 180
GROUP BY CP.DNCP_PATIENTID
) AS MAXSVCDATE
ON (CP.DNCP_PATIENTID = MAXSVCDATE.PSNNMBR AND CP.DNCP_FROMSERVICEDATE = MAXSVCDATE.SVCDT) --AND CP.DNCP_AMOUNT_CHARGE = MAXSVCDATE.CHGAMT)
WHERE CP.OFFICE_ID = 'EDQV'
As I'm not converting anything, I'm unsure why I'm getting this error. I've used a variation of this coding on many projects and never received the error. I appreciate any help. Thanks!
Sorry, I got it. I was thinking it was in my INNER JOIN but it was in one of the other joins. I was joining a PERSONNUMBER to a PERSONID. I fixed it and everything works smoothly now. A case of staring at it too long and not seeing what's right in front of you. Thank you for your time.
Try CONV(N,from_base,to_base),it converts numbers between different number bases. It returns a string representation of the number N, converted
from base from_base to to_base