Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL 2005 Query Assistance

Posted on 2010-11-19
4
Medium Priority
?
268 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

963 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