Solved

SQL 2005 Query Assistance

Posted on 2010-11-19
4
262 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
ID: 34178436

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
ID: 34178463
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
ID: 34178876
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
ID: 34218080
Solved issue but thanks for your suggestions and input.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

810 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