I would like to create a function which runs a specific query that will extract any DimPatientID that is listed within the DimTestPatient table. Currently, I use the WHERE clause to exclude the test IDs from my query. I would like a function because I will use the WHERE clause on almost any query that I will run in the future.
SELECT
COUNT(*)
FROM
dbo.DimPatient dp
LEFT JOIN
dbo.FactTreatmentHistory th ON dp.DimPatientID = th.DimPatientID
WHERE
dp.DimPatientID NOT IN (SELECT DimPatientID
FROM dbo.DimTestPatient)