Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL 2005 Query Assistance

Posted on 2010-11-19
4
Medium Priority
?
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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