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
Solved

SQL 2005 - Add in a count for unique PatientVisitIds per PatientProfileId

Posted on 2010-09-02
4
264 Views
Last Modified: 2012-05-10
First & foremost, the SQL is handled dynamically by the server, therefore the coding in my WHERE clauses will look odd to you. Please disregard this as its not an issue.

Currently, the SQL below works great and is returning results as I need and intend. My client has come back and asked me to add in an ability to pull patients with "X" number of visits between the date ranges they use in the report. Per my client, "if we selects 3, we want the report to only return patients that have had 3 visits in the given date range for visits".

I am pulling in the PatientProfile.PatientProfileId (which is the Primary Key and guaranteed to be unique for each patient) and the PatientVisit.PatientVisitId (Primary Key and guaranteed to be unique for each visit).

I need to have a column that adds a count for each PatientVisitId for each Patient and not sure how.
SET NOCOUNT ON

DECLARE 
	@startdate datetime,
	@enddate datetime,
	@groupby1 varchar(60),
	@groupby2 varchar(60)

SET @groupby1 = convert(varchar(60),'Diagnosis')
SET @groupby2 = convert(varchar(60),'(None)')

SET @startdate = ISNULL(NULL,'1/1/1900') 
SET @enddate = ISNULL(NULL,'1/1/3000')  

DECLARE 
	@DOBFrom datetime, 
	@DOBTo datetime

SET @DOBFrom =  ISNULL(NULL,'1/1/1900') 
SET @DOBTo = ISNULL(NULL,'1/1/3000')  

IF 1 = 1
BEGIN

SELECT DISTINCT 
	pv.PatientVisitId,
	pp.PatientId,
	ISNULL(pp.MedicalRecordNumber,'') as PatientMRN, 
	pp.PatientProfileId, 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '') AS Name,
	ISNULL(Convert(VarChar(20), pp.birthdate, 101),'No DOB')as PatientDOB,
	FLOOR(DATEDIFF(DAY, pp.birthdate, GETDATE()) / 365.25) AS [Patient Age],
	ISNULL(pp.sex,'')as Gender,
	ISNULL(race.Description,'Unknown') AS Race,
	dbo.cusfn_GetCPTCode(pv.PatientVisitId)AS CPTCode,
	/*ISNULL(pvp.CPTCode, 'No CPT Code') AS CPTCode,*/
	/*pvp.Code AS ProcCode,*/
	/*pvp.DateOfEntry,*/
	/*pvp.Description AS ProcDescription,*/
	/*pvd.code,*/
	dbo.cusfn_GetDXCode(pv.PatientVisitId)AS code,
	/*pvd.Description,*/
	/*pvd.Listorder,*/
	fac.OrgName AS Facility,
	dr.ListName,
	pv.Visit AS Visit,
	pv.TicketNumber,
	ISNULL(ic.ListName, 'Self Pay') AS [Insurance Carrier], 
	ISNULL(fc.description, 'No Fin. Class') AS FinancialClass,
	ISNULL(pt.Description, 'No Policy Type') AS PolicyType,
	CONVERT(VARCHAR(20),GETDATE(),101) + UPPER(pp.PatientProfileId) AS VisitKey,
	case @groupby1
		when 'Diagnosis' then pvd.code
		when 'CPT Code' then ISNULL(pvp.CPTCode, 'No CPT Code')
		else 'None'
	end as Group1,
	case @groupby2
		when 'Facility' then ISNULL(fac.listname, 'No Facility')
		when 'Provider' then ISNULL(dr.listname,'No Provider')
		when 'Policy Type' then ISNULL(pt.Description, 'No Policy Type') 
		when 'Insurance Carrier' then ISNULL(ic.ListName, 'Self Pay')
		else 'None'
	end as Group2
INTO #Tmp	

FROM 
	PatientVisit pv 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
	INNER JOIN DoctorFacility dr ON pv.DoctorId = dr.DoctorFacilityId
	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId
	LEFT JOIN medlists fc on pv.financialclassMID = fc.medlistsID
	LEFT OUTER JOIN InsuranceCarriers ic ON pv.CurrentInsuranceCarriersID = ic.InsuranceCarriersID
	LEFT OUTER JOIN MedLists pt ON ic.PolicyTypeMID = pt.MedListsID
	LEFT JOIN Medlists race ON pp.RaceMId = race.medlistsid
	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId
	INNER JOIN PatientVisitDiags pvd ON pv.patientvisitid = pvd.patientvisitid

WHERE
	ISNULL(pvp.Voided,0)= 0 AND
	(   
	('1' = '1' AND pvp.DateOfServiceFrom >= @startdate AND pvp.DateOfServiceFrom < @enddate+1) 
	OR
	('1' = '2' AND pvp.dateofentry >= @startdate AND pvp.dateofentry < @enddate+1)
	)
	AND --Filter on patient
	(
	(NULL IS NOT NULL AND  pp.PatientProfileID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Doctor
	(
	(NULL IS NOT NULL AND pv.DoctorID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Diagnosis
	(
	(NULL IS NOT NULL AND pvd.DiagnosisId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Procedure
	(
	(NULL IS NOT NULL AND pvp.ProceduresId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --Filter on Gender
	(
	(Null IS NOT NULL AND pp.sex in (Null)) OR
	(Null IS NULL)
	)
	AND --- Filter on Race
	(
	(NULL IS NOT NULL AND pp.racemid  IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --- Filter on PolicyType
	(
	(NULL IS NOT NULL AND ic.PolicyTypeMID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Insurance Carrier
	(
	(NULL IS NOT NULL AND pv.CurrentInsuranceCarriersId IN (NULL)) OR
	(NULL IS NULL)
	)

ORDER BY 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '')
	
SELECT 
	* 
FROM 
	#Tmp  

WHERE 	
	--Filter on Age
	(
	([Patient Age] >= ('-1') AND [Patient Age] <= ('125'))	
	)      

DROP TABLE #Tmp

END
ELSE
BEGIN

SELECT DISTINCT  
	pv.PatientVisitId,
	pp.PatientId,
	ISNULL(pp.MedicalRecordNumber,'') as PatientMRN, 
	pp.PatientProfileId, 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '') AS Name,
	ISNULL(Convert(VarChar(20), pp.birthdate, 101),'No DOB')as PatientDOB,
	FLOOR(DATEDIFF(DAY, pp.birthdate, GETDATE()) / 365.25) AS [Patient Age],
	ISNULL(pp.sex,'')as Gender,
	ISNULL(race.Description,'Unknown') AS Race,
	dbo.cusfn_GetCPTCode(pv.PatientVisitId)AS CPTCode,
	/*ISNULL(pvp.CPTCode, 'No CPT Code') AS CPTCode,*/
	/*pvp.Code AS ProcCode,*/
	/*pvp.DateOfEntry,*/
	/*pvp.Description AS ProcDescription,*/
	/*pvd.code,*/
	dbo.cusfn_GetDXCode(pv.PatientVisitId)AS code,
	/*pvd.Description,*/
	/*pvd.Listorder,*/
	fac.OrgName AS Facility,
	dr.ListName,
	pv.Visit AS Visit,
	pv.TicketNumber,
	ISNULL(ic.ListName, 'Self Pay') AS [Insurance Carrier], 
	ISNULL(fc.description, 'No Fin. Class') AS FinancialClass,
	ISNULL(pt.Description, 'No Policy Type') AS PolicyType,
	CONVERT(VARCHAR(20),GETDATE(),101) + UPPER(pp.PatientProfileId) AS VisitKey,
	case @groupby1
		when 'Diagnosis' then pvd.code
		when 'CPT Code' then ISNULL(pvp.CPTCode, 'No CPT Code')
		else 'None'
	end as Group1,
	case @groupby2
		when 'Facility' then ISNULL(fac.listname, 'No Facility')
		when 'Provider' then ISNULL(dr.listname,'No Provider')
		when 'Policy Type' then ISNULL(pt.Description, 'No Policy Type') 
		when 'Insurance Carrier' then ISNULL(ic.ListName, 'Self Pay')
		else 'None'
	end as Group2
	
FROM 
	PatientVisit pv 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
	INNER JOIN DoctorFacility dr ON pv.DoctorId = dr.DoctorFacilityId
	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId
	LEFT JOIN medlists fc on pv.financialclassMID = fc.medlistsID
	LEFT OUTER JOIN InsuranceCarriers ic ON pv.CurrentInsuranceCarriersID = ic.InsuranceCarriersID
	LEFT OUTER JOIN MedLists pt ON ic.PolicyTypeMID = pt.MedListsID
	LEFT JOIN Medlists race ON pp.RaceMId = race.medlistsid
	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId
	INNER JOIN PatientVisitDiags pvd ON pv.patientvisitid = pvd.patientvisitid

WHERE
	ISNULL(pvp.Voided,0)= 0 AND
	(   
	('1' = '1' AND pvp.DateOfServiceFrom >= @startdate AND pvp.DateOfServiceFrom < @enddate+1) 
    	OR
	('1' = '2' AND pvp.dateofentry >= @startdate AND pvp.dateofentry < @enddate+1)
	)
	AND --Filter on patient
	(
	(NULL IS NOT NULL AND  pp.PatientProfileID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Doctor
	(
	(NULL IS NOT NULL AND pv.DoctorID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Diagnosis
	(
	(NULL IS NOT NULL AND pvd.DiagnosisId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Procedure
	(
	(NULL IS NOT NULL AND pvp.ProceduresId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --Filter on Gender
	(
	(Null IS NOT NULL AND pp.sex in (Null)) OR
	(Null IS NULL)
	)
	AND --- Filter on Race
	(
	(NULL IS NOT NULL AND pp.racemid  IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --- Filter on PolicyType
	(
	(NULL IS NOT NULL AND ic.PolicyTypeMID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Insurance Carrier
	(
	(NULL IS NOT NULL AND pv.CurrentInsuranceCarriersId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on DOBDate
	(
	pp.Birthdate >= @DOBFrom  AND pp.Birthdate <= @DOBTo
	)
	
ORDER BY 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '')

END

Open in new window

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

Expert Comment

by:MPKR
ID: 33591872
This is a lot of code to work through... I can't provide you help as long as I dont have any idea where the data is stored inside.
But> Did you think of using a scalar function to return what you need inside of a column?
0
 
LVL 7

Author Comment

by:Jeff S
ID: 33591912
"I can't provide you help as long as I dont have any idea where the data is stored inside." - I don't understand your question.
"But> Did you think of using a scalar function to return what you need inside of a column? " - What do you mean by this?
0
 
LVL 4

Expert Comment

by:MPKR
ID: 33591953
I meant that you are using a lot of tables and you cant expect everyone works through all of that.
Here is what I can provide you from my mind without lookingeverything up, you can right cklick on FUNCTIONS / SCALAR VALUE FUNCTIONS in your Management console and create a new Function that looks similar like this below and find the needed count value per pationd inside of it. Then you can use it in your SELECt query...
create function GiveMeTheNoOfVisits(@IDPatient as int)
RETURNS int
as
Begin
RETURN SELECT COUNT(*) From ny Table joining another Table  ........
END
Usage is like this>
SELECT A, B, C, dbo.GiveMeTheNoOfVisits(Table.PatientID) as CounterVisits From TABLE ...
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 33601376
This will be no worse than what you already have (which incidentaly must be quite a dog):
SET NOCOUNT ON

DECLARE 
	@startdate datetime,
	@enddate datetime,
	@groupby1 varchar(60),
	@groupby2 varchar(60)

SET @groupby1 = convert(varchar(60),'Diagnosis')
SET @groupby2 = convert(varchar(60),'(None)')

SET @startdate = ISNULL(NULL,'1/1/1900') 
SET @enddate = ISNULL(NULL,'1/1/3000')  

DECLARE 
	@DOBFrom datetime, 
	@DOBTo datetime

SET @DOBFrom =  ISNULL(NULL,'1/1/1900') 
SET @DOBTo = ISNULL(NULL,'1/1/3000')  

IF 1 = 1
BEGIN

SELECT 
	pv.PatientVisitId,
	pp.PatientId,
	ISNULL(pp.MedicalRecordNumber,'') as PatientMRN, 
	pp.PatientProfileId, 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '') AS Name,
	ISNULL(Convert(VarChar(20), pp.birthdate, 101),'No DOB')as PatientDOB,
	FLOOR(DATEDIFF(DAY, pp.birthdate, GETDATE()) / 365.25) AS [Patient Age],
	ISNULL(pp.sex,'')as Gender,
	ISNULL(race.Description,'Unknown') AS Race,
	dbo.cusfn_GetCPTCode(pv.PatientVisitId)AS CPTCode,
	/*ISNULL(pvp.CPTCode, 'No CPT Code') AS CPTCode,*/
	/*pvp.Code AS ProcCode,*/
	/*pvp.DateOfEntry,*/
	/*pvp.Description AS ProcDescription,*/
	/*pvd.code,*/
	dbo.cusfn_GetDXCode(pv.PatientVisitId)AS code,
	/*pvd.Description,*/
	/*pvd.Listorder,*/
	fac.OrgName AS Facility,
	dr.ListName,
	pv.Visit AS Visit,
	pv.TicketNumber,
	ISNULL(ic.ListName, 'Self Pay') AS [Insurance Carrier], 
	ISNULL(fc.description, 'No Fin. Class') AS FinancialClass,
	ISNULL(pt.Description, 'No Policy Type') AS PolicyType,
	CONVERT(VARCHAR(20),GETDATE(),101) + UPPER(pp.PatientProfileId) AS VisitKey,
	case @groupby1
		when 'Diagnosis' then pvd.code
		when 'CPT Code' then ISNULL(pvp.CPTCode, 'No CPT Code')
		else 'None'
	end as Group1,
	case @groupby2
		when 'Facility' then ISNULL(fac.listname, 'No Facility')
		when 'Provider' then ISNULL(dr.listname,'No Provider')
		when 'Policy Type' then ISNULL(pt.Description, 'No Policy Type') 
		when 'Insurance Carrier' then ISNULL(ic.ListName, 'Self Pay')
		else 'None'
	end as Group2
INTO #Tmp	

FROM 
	PatientVisit pv 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
	INNER JOIN DoctorFacility dr ON pv.DoctorId = dr.DoctorFacilityId
	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId
	LEFT JOIN medlists fc on pv.financialclassMID = fc.medlistsID
	LEFT OUTER JOIN InsuranceCarriers ic ON pv.CurrentInsuranceCarriersID = ic.InsuranceCarriersID
	LEFT OUTER JOIN MedLists pt ON ic.PolicyTypeMID = pt.MedListsID
	LEFT JOIN Medlists race ON pp.RaceMId = race.medlistsid
	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId
	INNER JOIN PatientVisitDiags pvd ON pv.patientvisitid = pvd.patientvisitid

WHERE
	ISNULL(pvp.Voided,0)= 0 AND
	(   
	('1' = '1' AND pvp.DateOfServiceFrom >= @startdate AND pvp.DateOfServiceFrom < @enddate+1) 
	OR
	('1' = '2' AND pvp.dateofentry >= @startdate AND pvp.dateofentry < @enddate+1)
	)
	AND --Filter on patient
	(
	(NULL IS NOT NULL AND  pp.PatientProfileID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Doctor
	(
	(NULL IS NOT NULL AND pv.DoctorID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Diagnosis
	(
	(NULL IS NOT NULL AND pvd.DiagnosisId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Procedure
	(
	(NULL IS NOT NULL AND pvp.ProceduresId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --Filter on Gender
	(
	(Null IS NOT NULL AND pp.sex in (Null)) OR
	(Null IS NULL)
	)
	AND --- Filter on Race
	(
	(NULL IS NOT NULL AND pp.racemid  IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --- Filter on PolicyType
	(
	(NULL IS NOT NULL AND ic.PolicyTypeMID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Insurance Carrier
	(
	(NULL IS NOT NULL AND pv.CurrentInsuranceCarriersId IN (NULL)) OR
	(NULL IS NULL)
	)
GROUP BY
	pv.PatientVisitId,
	pp.PatientId,
	ISNULL(pp.MedicalRecordNumber,''), 
	pp.PatientProfileId, 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], ''),
	ISNULL(Convert(VarChar(20), pp.birthdate, 101),'No DOB'),
	FLOOR(DATEDIFF(DAY, pp.birthdate, GETDATE()) / 365.25),
	ISNULL(pp.sex,''),
	ISNULL(race.Description,'Unknown'),
	dbo.cusfn_GetCPTCode(pv.PatientVisitId),
	dbo.cusfn_GetDXCode(pv.PatientVisitId),
	fac.OrgName,
	dr.ListName,
	pv.Visit,
	pv.TicketNumber,
	ISNULL(ic.ListName, 'Self Pay'), 
	ISNULL(fc.description, 'No Fin. Class'),
	ISNULL(pt.Description, 'No Policy Type'),
	CONVERT(VARCHAR(20),GETDATE(),101) + UPPER(pp.PatientProfileId),
	case @groupby1
		when 'Diagnosis' then pvd.code
		when 'CPT Code' then ISNULL(pvp.CPTCode, 'No CPT Code')
		else 'None'
	end,
	case @groupby2
		when 'Facility' then ISNULL(fac.listname, 'No Facility')
		when 'Provider' then ISNULL(dr.listname,'No Provider')
		when 'Policy Type' then ISNULL(pt.Description, 'No Policy Type') 
		when 'Insurance Carrier' then ISNULL(ic.ListName, 'Self Pay')
		else 'None'
	end
HAVING COUNT(*) = 3
ORDER BY 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '')
	
SELECT 
	* 
FROM 
	#Tmp  

WHERE 	
	--Filter on Age
	(
	([Patient Age] >= ('-1') AND [Patient Age] <= ('125'))	
	)      

DROP TABLE #Tmp

END
ELSE
BEGIN

SELECT DISTINCT  
	pv.PatientVisitId,
	pp.PatientId,
	ISNULL(pp.MedicalRecordNumber,'') as PatientMRN, 
	pp.PatientProfileId, 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '') AS Name,
	ISNULL(Convert(VarChar(20), pp.birthdate, 101),'No DOB')as PatientDOB,
	FLOOR(DATEDIFF(DAY, pp.birthdate, GETDATE()) / 365.25) AS [Patient Age],
	ISNULL(pp.sex,'')as Gender,
	ISNULL(race.Description,'Unknown') AS Race,
	dbo.cusfn_GetCPTCode(pv.PatientVisitId)AS CPTCode,
	/*ISNULL(pvp.CPTCode, 'No CPT Code') AS CPTCode,*/
	/*pvp.Code AS ProcCode,*/
	/*pvp.DateOfEntry,*/
	/*pvp.Description AS ProcDescription,*/
	/*pvd.code,*/
	dbo.cusfn_GetDXCode(pv.PatientVisitId)AS code,
	/*pvd.Description,*/
	/*pvd.Listorder,*/
	fac.OrgName AS Facility,
	dr.ListName,
	pv.Visit AS Visit,
	pv.TicketNumber,
	ISNULL(ic.ListName, 'Self Pay') AS [Insurance Carrier], 
	ISNULL(fc.description, 'No Fin. Class') AS FinancialClass,
	ISNULL(pt.Description, 'No Policy Type') AS PolicyType,
	CONVERT(VARCHAR(20),GETDATE(),101) + UPPER(pp.PatientProfileId) AS VisitKey,
	case @groupby1
		when 'Diagnosis' then pvd.code
		when 'CPT Code' then ISNULL(pvp.CPTCode, 'No CPT Code')
		else 'None'
	end as Group1,
	case @groupby2
		when 'Facility' then ISNULL(fac.listname, 'No Facility')
		when 'Provider' then ISNULL(dr.listname,'No Provider')
		when 'Policy Type' then ISNULL(pt.Description, 'No Policy Type') 
		when 'Insurance Carrier' then ISNULL(ic.ListName, 'Self Pay')
		else 'None'
	end as Group2
	
FROM 
	PatientVisit pv 
	INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
	INNER JOIN DoctorFacility dr ON pv.DoctorId = dr.DoctorFacilityId
	INNER JOIN DoctorFacility fac ON pv.FacilityId = fac.DoctorFacilityId
	LEFT JOIN medlists fc on pv.financialclassMID = fc.medlistsID
	LEFT OUTER JOIN InsuranceCarriers ic ON pv.CurrentInsuranceCarriersID = ic.InsuranceCarriersID
	LEFT OUTER JOIN MedLists pt ON ic.PolicyTypeMID = pt.MedListsID
	LEFT JOIN Medlists race ON pp.RaceMId = race.medlistsid
	INNER JOIN PatientVisitProcs pvp ON pv.PatientVisitId = pvp.PatientVisitId
	INNER JOIN PatientVisitDiags pvd ON pv.patientvisitid = pvd.patientvisitid

WHERE
	ISNULL(pvp.Voided,0)= 0 AND
	(   
	('1' = '1' AND pvp.DateOfServiceFrom >= @startdate AND pvp.DateOfServiceFrom < @enddate+1) 
    	OR
	('1' = '2' AND pvp.dateofentry >= @startdate AND pvp.dateofentry < @enddate+1)
	)
	AND --Filter on patient
	(
	(NULL IS NOT NULL AND  pp.PatientProfileID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Doctor
	(
	(NULL IS NOT NULL AND pv.DoctorID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Facility
	(
	(NULL IS NOT NULL AND pv.FacilityID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Diagnosis
	(
	(NULL IS NOT NULL AND pvd.DiagnosisId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Procedure
	(
	(NULL IS NOT NULL AND pvp.ProceduresId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --Filter on Gender
	(
	(Null IS NOT NULL AND pp.sex in (Null)) OR
	(Null IS NULL)
	)
	AND --- Filter on Race
	(
	(NULL IS NOT NULL AND pp.racemid  IN (NULL)) OR
	(NULL IS NULL)
	)
	AND --- Filter on PolicyType
	(
	(NULL IS NOT NULL AND ic.PolicyTypeMID IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on Insurance Carrier
	(
	(NULL IS NOT NULL AND pv.CurrentInsuranceCarriersId IN (NULL)) OR
	(NULL IS NULL)
	)
	AND  --Filter on DOBDate
	(
	pp.Birthdate >= @DOBFrom  AND pp.Birthdate <= @DOBTo
	)
	
ORDER BY 
	pp.[Last] + ', ' + pp.[First] + ' ' + ISNULL(pp.[Middle], '')

END

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

828 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