I want to count future Appointments made on the same day of an active appointment by Location. I expect multiple counts per Patient_ID given a date range. I am not sure if I need a temp table or if a subquery would work.
From the code below this is the error I get:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Definitions:
- Appointment_DateTime - (Date) is the actual appointment event
- DateTime_Scheduled - (Date) is the logging timestamp of future appointments
- Description - (text) is the Location Description
- Patient_ID - (int) is the unique patient ID
- Appointment_ID - (int) is the unique Appointment ID
SQL
SELECT
loc.Description
,Count(app.Appointment_ID)
FROM [Ntier_HARH].[PM].[Appointments] app
join [Ntier_HARH].[PM].[Resources] res
on res.Resource_ID = app.Resource_ID
join [Ntier_HARH].[PM].[Practitioners] doc
on doc.Practitioner_ID = res.Practitioner_ID
join [Ntier_HARH].[PM].[Scheduling_Locations] loc
on loc.Scheduling_Location_ID = app.Scheduling_Location_ID
where
cast(app.DateTime_Scheduled as date) = '2017-01-16'
and app.status <> 'X'
and cast(app.Appointment_DateTime as date) =
(Select cast(DateTime_Scheduled as date)
from [Ntier_HARH].[PM].[Appointments]
where Patient_ID = app.Patient_ID)
group by loc.Description
minormaxaggregate in sub query or you can useRow_Numberto avoid sub-query