Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help - Returning Customer with last x days
Message
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Replication
Title:
Help - Returning Customer with last x days
Miscellaneous
Thread ID:
00869960
Message ID:
00869960
Views:
64
I am trying to create a query based on the user entering a set of dates and a number of previous days, that will show all customers/patients who have had a case within the date range, and and been a case within the x days before the case within the date range. Here is what I've tried so far, but I get everyone not just the returning patients.
declare
@QueryFrom DateTime,
@QueryTo DateTime,
@tiPastDays int


set @QueryFrom = '01/01/2004 00:00:01'
set @QueryTo   = '01/24/2004 23:59:59'
set @tiPastDays = 30


select meetingnumber,begintime,BeginTime,EnterOR,ExitOR,
	(Select rtrim(Lastname)+', '+rtrim(FirstName) from patients where patientID=meetings.PatientID) as PatientName,
	(Select mpinumber from patients where patientID=meetings.PatientID) as medRecNo,
	(SELECT TOP 1 RTRIM(dbo.coper.lastname) + ', ' + RTRIM(dbo.coper.firstname)
	   FROM dbo.meetproc INNER JOIN
                dbo.coper ON dbo.meetproc.coperid = dbo.coper.coperid
                where dbo.meetproc.meetingnumber=meetings.meetingnumber
	   ORDER BY dbo.meetproc.sysorder) as SurgeonName,
	(SELECT TOP 1 meetproc.description
	   FROM dbo.meetproc 
                where dbo.meetproc.meetingnumber=meetings.meetingnumber
	   ORDER BY dbo.meetproc.sysorder) as PrimaryProc,
	(Select deptdesc from coDept where coDept.DeptID=meetings.DeptID) as DepartmentDesc,
	meetings.deptid
from meetings
where begintime between @QueryFrom and @QueryTo
and iscompleted=1
and patientid in (Select patientid 
	from meetings m1 
	where m1.iscompleted=1 
	and datediff(dd,m1.begintime,meetings.BeginTime)<=30
	and m1.meetingnumber <> meetings.BeginTime)
--order by departmentdesc,begintime
Thanks for any help
Next
Reply
Map
View

Click here to load this message in the networking platform