简体   繁体   中英

More than 1 appointment on the same day for a patient and display both appointments

I am trying to find patients that have more than 1 appointment on the same day. I want to then display all the appointments the patient may have. Do I need to use a subquery to do this? Here is what I have so far:

Select
  Appt.ID-PatNm as Patient,
  ApptNum,
  Sched_ApptType.Prov.Mnemonic as Type,
  Appt.Provider-Name as Provider, 
  Appt.Dt, 
  Appt.Tm,
  Appt.Department-Mnemonic As Dept,
  Appt.SchedulerInits,
  Case $EXTRACT(Appt.InternalStatus,1)
    when 'P' then 'Pending'
    when 'A' then 'Arrived' 
    when 'R' then 'Rescheduled'
  End as Status
From Sched.Appointment Appt 
JOIN Sched_ApptType.Prov ON
  Appt.Department = Sched_ApptType.Prov.Department
  and
  Appt.Provider =  Sched_ApptType.Prov.Provider
  and
  Appt.Type = Sched_ApptType.Prov.ApptType
Where (Appt.Dt) > DATEADD('DD',-120,CURRENT_DATE)
  AND Appt.InternalStatus IN ('P','R','A')
  AND Appt.Department-Mnemonic= 'EYE'
Group By 
  Appt.ID-PatNm,
  Appt.Dt

You get the patients having more than one appointment in a day by grouping by patient and day:

select distinct a.id_patnm
from sched.appointment a 
group by a.id_patnm, a.dt
having count(*) > 1

So yes, you need a subquery:

Where (Appt.Dt) > DATEADD('DD',-120,CURRENT_DATE)
  AND Appt.InternalStatus IN ('P','R','A')
  AND Appt.Department_Mnemonic= 'EYE'
  AND Appt.ID_PatNm IN
  (
    select a.id_patnm
    from sched.appointment a 
    group by a.id_patnm, a.dt
    having count(*) > 1
  )

(BTW: I used id_patnm instead of id-patnm here, for I don't know any DBMS that would allow the hyphen. When using a hyphen in a column name you have to use quotes on the name, eg "id-patnm" .)

我想您可以为Appointment_id添加一列,然后让您获得所需的结果。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM