SQL 2005 Query Assistance

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

LVL 7
Jeff SAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
LowfatspreadConnect With a Mentor Commented:
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
 
Bhavesh ShahLead AnalysistCommented:

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
 
Jeff SAuthor Commented:
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
 
Jeff SAuthor Commented:
Solved issue but thanks for your suggestions and input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.