Solved

SQL 2005 Query Assistance

Posted on 2010-11-19
4
259 Views
Last Modified: 2012-05-10
I am trying to capture the visits where (99201 - 99205) or (99211 - 99215) was billed with an Injection code (20605 or 20610) and a modifier 25 was lacking on the (99201 - 99205) or (99211 - 99215) code.
SET NOCOUNT ON



CREATE TABLE #Bill

	(

	patientvisitid int

	)

INSERT #Bill



SELECT DISTINCT  

	pv.patientvisitid

	

FROM 

	PatientVisit pv

	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 

	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId 

	LEFT OUTER JOIN MedLists ml2 ON pvp.Modifier1MId = ml2.MedListsId 

	LEFT OUTER JOIN MedLists ml3 ON pvp.Modifier2MId = ml3.MedListsId 

	LEFT OUTER JOIN MedLists ml4 ON pvp.Modifier3MId = ml4.MedListsId 

	LEFT OUTER JOIN MedLists ml5 ON pvp.Modifier4MId = ml5.MedListsId 

	INNER JOIN DoctorFacility doc ON pv.DoctorId = doc.DoctorFacilityId 

	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId 

	LEFT JOIN InsuranceCarriers ic ON pv.PrimaryInsuranceCarriersId = ic.InsuranceCarriersId 

	INNER JOIN MedLists ml ON pv.BillStatus = ml.JoinId 

	LEFT OUTER JOIN PatientVisitDiags pvd ON pv.PatientVisitId = pvd.PatientVisitId AND pvp.PatientVisitDiags1 = pvd.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd1 ON pv.PatientVisitId = pvd1.PatientVisitId AND pvp.PatientVisitDiags2 = pvd1.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd2 ON pv.PatientVisitId = pvd2.PatientVisitId AND pvp.PatientVisitDiags3 = pvd2.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd3 ON pv.PatientVisitId = pvd3.PatientVisitId AND pvp.PatientVisitDiags4 = pvd3.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd4 ON pv.PatientVisitId = pvd4.PatientVisitId AND pvp.PatientVisitDiags5 = pvd4.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd5 ON pv.PatientVisitId = pvd5.PatientVisitId AND pvp.PatientVisitDiags6 = pvd5.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd6 ON pv.PatientVisitId = pvd6.PatientVisitId AND pvp.PatientVisitDiags7 = pvd6.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd7 ON pv.PatientVisitId = pvd7.PatientVisitId AND pvp.PatientVisitDiags8 = pvd7.ListOrder 	

	

WHERE

	(ml.TableName = 'BillStatus') 

	AND ml.JoinId IN (5, 2)

	AND ((pvp.CPTCode >= '99201' And pvp.CPTCode <= '99205') OR

        (pvp.CPTCode >= '99211' AND pvp.CPTCode <= '99215')) 	





SELECT

	pp.PatientId, 

	dbo.FormatName(pp.Prefix, pp.First, pp.Middle, pp.Last, pp.Suffix) AS PatientName,

	pv.TicketNumber,

	pvp.DateofServiceFrom, 

	ml.Description, 

	fac.ListName,

	doc.ListName,

	ISNULL(ic.ListName, 'Self Pay') AS Insurance,

	pvp.CPTCode, 

	ISNULL(CONVERT(varchar(4), ml2.Code), ' ') 

	+ ' ' + ISNULL(CONVERT(varchar(4), ml3.Code), ' ') 

	+ ' ' + ISNULL(CONVERT(varchar(4), ml4.Code), ' ')

	+ ' ' + ISNULL(CONVERT(varchar(4), ml5.Code), ' ') AS Modifier,

	ISNULL(pvd.Code,'') AS Diag1, 

	ISNULL(pvd1.Code,'') AS Diag2, 

	ISNULL(pvd2.Code,'') AS Diag3,

	ISNULL(pvd3.Code,'') AS Diag4,

	ISNULL(pvd4.Code,'') AS Diag5,

	ISNULL(pvd5.Code,'') AS Diag6,

	ISNULL(pvd6.Code,'') AS Diag7,

	ISNULL(pvd7.Code,'') AS Diag8

	

	

FROM

	PatientVisit pv

	INNER JOIN #Bill b ON pv.PatientVisitId = b.PatientVisitId

	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 

	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId 

	LEFT OUTER JOIN MedLists ml2 ON pvp.Modifier1MId = ml2.MedListsId 

	LEFT OUTER JOIN MedLists ml3 ON pvp.Modifier2MId = ml3.MedListsId 

	LEFT OUTER JOIN MedLists ml4 ON pvp.Modifier3MId = ml4.MedListsId 

	LEFT OUTER JOIN MedLists ml5 ON pvp.Modifier4MId = ml5.MedListsId 

	INNER JOIN DoctorFacility doc ON pv.DoctorId = doc.DoctorFacilityId 

	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId 

	LEFT JOIN InsuranceCarriers ic ON pv.PrimaryInsuranceCarriersId = ic.InsuranceCarriersId 

	INNER JOIN MedLists ml ON pv.BillStatus = ml.JoinId 

	LEFT OUTER JOIN PatientVisitDiags pvd ON pv.PatientVisitId = pvd.PatientVisitId AND pvp.PatientVisitDiags1 = pvd.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd1 ON pv.PatientVisitId = pvd1.PatientVisitId AND pvp.PatientVisitDiags2 = pvd1.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd2 ON pv.PatientVisitId = pvd2.PatientVisitId AND pvp.PatientVisitDiags3 = pvd2.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd3 ON pv.PatientVisitId = pvd3.PatientVisitId AND pvp.PatientVisitDiags4 = pvd3.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd4 ON pv.PatientVisitId = pvd4.PatientVisitId AND pvp.PatientVisitDiags5 = pvd4.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd5 ON pv.PatientVisitId = pvd5.PatientVisitId AND pvp.PatientVisitDiags6 = pvd5.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd6 ON pv.PatientVisitId = pvd6.PatientVisitId AND pvp.PatientVisitDiags7 = pvd6.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd7 ON pv.PatientVisitId = pvd7.PatientVisitId AND pvp.PatientVisitDiags8 = pvd7.ListOrder 



WHERE

	(ml.TableName = 'BillStatus') 

 

DROP TABLE #Bill

Open in new window

0
Comment
Question by:Jeff S
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Bhavesh Shah
Comment Utility

Hi,

Injection code (20605 or 20610) and a modifier 25 was lacking on the (99201 - 99205) or (99211 - 99215) code.

above condition is pending to put??
0
 
LVL 7

Author Comment

by:Jeff S
Comment Utility
I want to get back the vists that had a code (99201-99205 or 99211-99215) with (20605 or 20610) and on the (99201-99205 or 99211-99215) it lacked '25' in the Modifier field.
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
Comment Utility
ok

which table is the injection code on (and column name)?
also confirm the column name and table for the modifier....

when setting up the #bill table yoou don't need the outer joined table conditions...
should be more like my example...

the second select is showing all information for the patients identified in the #bill table

what did you actually want?

to show all info for patients with the criteria... or just to show the  patientvist info for the initial criteria?
SET NOCOUNT ON



CREATE TABLE #Bill

	(

	patientvisitid int

	)

INSERT #Bill



SELECT DISTINCT  

	pv.patientvisitid

	

FROM 

	PatientVisit pv

	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId  

		

	

WHERE pv.BillStatus in (2,5)

  AND (    pvp.CPTCode between '99201' and '99205'

        OR pvp.CPTCode between '99211' AND '99215'

      ) 	

  and '25' not in (pvp.Modifier1MId,pvp.Modifier2MId,pvp.Modifier3MId,pvp.Modifier4MId)

  and pvp.injectioncode in ('20605','20610')



SELECT

	pp.PatientId, 

	dbo.FormatName(pp.Prefix, pp.First, pp.Middle, pp.Last, pp.Suffix) AS PatientName,

	pv.TicketNumber,

	pvp.DateofServiceFrom, 

	ml.Description, 

	fac.ListName,

	doc.ListName,

	ISNULL(ic.ListName, 'Self Pay') AS Insurance,

	pvp.CPTCode, 

	ISNULL(CONVERT(varchar(4), ml2.Code), ' ') 

	+ ' ' + ISNULL(CONVERT(varchar(4), ml3.Code), ' ') 

	+ ' ' + ISNULL(CONVERT(varchar(4), ml4.Code), ' ')

	+ ' ' + ISNULL(CONVERT(varchar(4), ml5.Code), ' ') AS Modifier,

	ISNULL(pvd.Code,'') AS Diag1, 

	ISNULL(pvd1.Code,'') AS Diag2, 

	ISNULL(pvd2.Code,'') AS Diag3,

	ISNULL(pvd3.Code,'') AS Diag4,

	ISNULL(pvd4.Code,'') AS Diag5,

	ISNULL(pvd5.Code,'') AS Diag6,

	ISNULL(pvd6.Code,'') AS Diag7,

	ISNULL(pvd7.Code,'') AS Diag8

	

	

FROM

	PatientVisit pv

	INNER JOIN #Bill b ON pv.PatientVisitId = b.PatientVisitId

	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 

	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId 

	LEFT OUTER JOIN MedLists ml2 ON pvp.Modifier1MId = ml2.MedListsId 

	LEFT OUTER JOIN MedLists ml3 ON pvp.Modifier2MId = ml3.MedListsId 

	LEFT OUTER JOIN MedLists ml4 ON pvp.Modifier3MId = ml4.MedListsId 

	LEFT OUTER JOIN MedLists ml5 ON pvp.Modifier4MId = ml5.MedListsId 

	INNER JOIN DoctorFacility doc ON pv.DoctorId = doc.DoctorFacilityId 

	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId 

	LEFT JOIN InsuranceCarriers ic ON pv.PrimaryInsuranceCarriersId = ic.InsuranceCarriersId 

	INNER JOIN MedLists ml ON pv.BillStatus = ml.JoinId 

	LEFT OUTER JOIN PatientVisitDiags pvd ON pv.PatientVisitId = pvd.PatientVisitId AND pvp.PatientVisitDiags1 = pvd.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd1 ON pv.PatientVisitId = pvd1.PatientVisitId AND pvp.PatientVisitDiags2 = pvd1.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd2 ON pv.PatientVisitId = pvd2.PatientVisitId AND pvp.PatientVisitDiags3 = pvd2.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd3 ON pv.PatientVisitId = pvd3.PatientVisitId AND pvp.PatientVisitDiags4 = pvd3.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd4 ON pv.PatientVisitId = pvd4.PatientVisitId AND pvp.PatientVisitDiags5 = pvd4.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd5 ON pv.PatientVisitId = pvd5.PatientVisitId AND pvp.PatientVisitDiags6 = pvd5.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd6 ON pv.PatientVisitId = pvd6.PatientVisitId AND pvp.PatientVisitDiags7 = pvd6.ListOrder 

	LEFT OUTER JOIN PatientVisitDiags pvd7 ON pv.PatientVisitId = pvd7.PatientVisitId AND pvp.PatientVisitDiags8 = pvd7.ListOrder 



WHERE

	(ml.TableName = 'BillStatus') 

 

DROP TABLE #Bill

Open in new window

0
 
LVL 7

Author Closing Comment

by:Jeff S
Comment Utility
Solved issue but thanks for your suggestions and input.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Backup & Restore 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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

10 Experts available now in Live!

Get 1:1 Help Now