I am trying to create a report that, among other things, lists how many new patients were seen in a particular month. A "new" patient is someone who has either 1) never been seen before, or 2) has been seen before, but had their last visit more than three years ago. I am currently trying to do this with a cursor that calls a table-valued function that reports the number of new patients seen on a given date. The cursor calls the function for each visit date in the last six years, and the execution time is abyssmal. Listed below are the cursor query logic, and below it the table-valued function definition. I also include the definition of the indexed view "vwKeptVisits" referenced by the function. Finally, I give some sample query results. These results are what I am trying to produce with the query, just looking for a more efficient way to do it... I am looking for ideas on how to do this without a cursor.
//CURSOR LOGIC//
DECLARE @BEGINYEAR INT;
SET @BEGINYEAR = YEAR(GETDATE()) - 6;
DECLARE @TBLNewPts TABLE(
VisitDate SMALLDATETIME,
NewPtCount INT
)
DECLARE @VisitDate SMALLDATETIME
DECLARE TblCursor CURSOR
FAST_FORWARD
FOR
SELECT DISTINCT VisitDate
FROM dbo.Visits
WHERE YEAR(VisitDate) > @BEGINYEAR
OPEN TblCursor
FETCH NEXT FROM TblCursor INTO @VisitDate
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT @TBLNewPts
SELECT
@VisitDate,
ISNULL(NewPts, 0)
FROM dbo.fnNewPatientsByVisitDate(@VisitDate)
FETCH NEXT FROM TblCursor INTO @VisitDate
END
CLOSE TblCursor
DEALLOCATE TblCursor;
///////////////////////////////////////////////////////////////////////////////////////////////
CREATE FUNCTION [dbo].[fnNewPatientsByVisitDate]
(
@VisitDate SMALLDATETIME
)
RETURNS
@tbl TABLE
(
NewPts INT
)
WITH SCHEMABINDING
AS
BEGIN
DECLARE @TBLNewPts TABLE(
VisitDate SMALLDATETIME,
PtID INT
)
INSERT @TBLNewPts
--PATIENTS WHO HAVE NEVER BEEN SEEN BEFORE @VISITDATE
SELECT
@VisitDate,
PtID
FROM dbo.vwKeptVisits
WHERE (VisitDate = @VisitDate)
AND
(PtID NOT IN
(SELECT PtID
FROM dbo.vwKeptVisits
WHERE VisitDate < @VisitDate)
)
UNION
--PATIENTS SEEN BEFORE, BUT MORE THAN THREE YEARS BEFORE @VISITDATE
SELECT
@VisitDate,
PtID
FROM dbo.vwKeptVisits
WHERE (VisitDate = @VisitDate)
AND
(PtID NOT IN
(SELECT PREVIOUS.PtID
FROM dbo.vwKeptVisits AS PREVIOUS INNER JOIN
dbo.vwKeptVisits AS CURRNT ON
CURRNT.PtID = PREVIOUS.PtID AND
CURRNT.VisitNumber > PREVIOUS.VisitNumber
WHERE (PREVIOUS.VisitDate > DATEADD(YEAR,-3,@VisitDate))
));
INSERT @tbl
SELECT
COUNT(PtID)
FROM @TBLNewPts ;
RETURN
END
/////////////////////////////////////////////////////////////////////////////////////////
/****** Object: View [dbo].[vwKeptVisits] Script Date: 6/18/2024 8:31:05 PM ******/
CREATE VIEW [dbo].[vwKeptVisits]
WITH SCHEMABINDING
AS
SELECT
COUNT_BIG(*) AS ServiceCount,
V.VisitNumber,
V.PtID,
V.VisitDate,
V.MDID,
V.ApptID,
V.DX1,
V.DX2,
v.DX3,
v.DX4,
V.DX5,
V.POS
FROM DBO.Visits V INNER JOIN
dbo.[Visit Details] VD ON
V.VisitNumber = VD.VisitNumber
WHERE (V.Void = 0) AND
(VD.[CPT CODE] <> 'No Show' AND
VD.[CPT CODE] <> 'MD Cancel' AND
VD.[CPT CODE] <> 'Pt Cancel') AND
(V.ApptID IS NOT NULL)
GROUP BY V.VisitNumber, v.PtID , v.VisitDate, V.MDID, V.ApptID,
V.DX1, V.DX2, v.DX3, v.DX4, V.DX5, V.POS
GO
GRANT SELECT ON [dbo].[vwKeptVisits] TO [db_PracManUserRole] AS [dbo]
GO
SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET NUMERIC_ROUNDABORT OFF
GO
/****** Object: Index [PK_vwKeptVisits] Script Date: 6/18/2024 8:31:05 PM ******/
CREATE UNIQUE CLUSTERED INDEX [PK_vwKeptVisits] ON [dbo].[vwKeptVisits]
(
[VisitNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [INDICESGROUP]
GO
SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET NUMERIC_ROUNDABORT OFF
GO
/****** Object: Index [IX_vwKeptVisits_MDID] Script Date: 6/18/2024 8:31:05 PM ******/
CREATE NONCLUSTERED INDEX [IX_vwKeptVisits_MDID] ON [dbo].[vwKeptVisits]
(
[MDID] ASC
)
INCLUDE([PtID],[VisitDate]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [INDICESGROUP]
GO
SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET NUMERIC_ROUNDABORT OFF
GO
/****** Object: Index [IX_vwKeptVisits_PtID_VisitDate] Script Date: 6/18/2024 8:31:05 PM ******/
CREATE NONCLUSTERED INDEX [IX_vwKeptVisits_PtID_VisitDate] ON [dbo].[vwKeptVisits]
(
[PtID] ASC,
[VisitDate] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 96, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [INDICESGROUP]
GO
////SAMPLE QUERY RESULTS///////
VisitDate NewPtCount
2019-01-02 00:00:00 0
2019-01-03 00:00:00 0
2019-01-04 00:00:00 0
2019-01-07 00:00:00 2
2019-01-08 00:00:00 3
2019-01-09 00:00:00 0
2019-01-10 00:00:00 4
2019-01-11 00:00:00 5
2019-01-14 00:00:00 1
2019-01-15 00:00:00 0
2019-01-16 00:00:00 0
2019-01-17 00:00:00 0
2019-01-18 00:00:00 0
2019-01-21 00:00:00 0
2019-01-22 00:00:00 1
The current query time is more than 35 seconds.
I'm not clear why the values are being tabulated for six full years of dates. All you need to do is check that no visit exists within the prior three years for the dates that fall within the report:
select VisitDate, count(*) as NewPtCnt
from dbo.vwKeptVisits v
where VisitDate between @reportStart and @reportEnd
and not exists (
select 1
from dbo.vwKeptVisits v2
where v2.PatId = v.PatId
and v2.VisitDate between dateadd(year, -3, v.VisitDate) and dateadd(day, -1, v.VisitDate)
)
group by VisitDate;
You should use a date
type rather than smalldatetime
. Adjust the ends of the ranges as necessary for the values being stored.