> ## Documentation Index
> Fetch the complete documentation index at: https://help.elationhealth.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Forms required by an appointment type

> The patient forms set on each appointment type, which decide the forms an appointment asks its patient to fill in.

The `patient_form_appointment_type` table has one row per form set on an appointment type. These settings are what make an appointment ask its patient for forms: book an appointment of a type, and the type's current forms become the forms that appointment requires. Join `patient_form_id` to `patient_form.id`, `practice_id` to `practice.id`, and `appt_type_id` to `appointment.appt_type_id`.

Staff can override the list for a single appointment, which is recorded in [Per-appointment form changes](https://help.elationhealth.com/articles/hdb/patient-form-appointment-change.md). For how practices set forms on appointment types, see the [Patient Forms Guide](https://help.elationhealth.com/articles/Patient-Forms-Guide.md).

This table's columns and relationships are shown in the [Hosted Database schema](https://help.elationhealth.com/articles/hdb/schema.md).

**For reporting on but not limited to:**

* The forms an appointment type currently asks for
* The forms an appointment requires of its patient
* Forms that have been removed from an appointment type, and when
* Which appointment types ask for the most forms

## Before you query forms on appointment types

### The current setup is the rows where `deletion_time` is null

Those rows are the appointment type's current forms, in `sequence` order. Everything else is history.

### Elation replaces every row when a type's forms are saved

Each time a practice saves an appointment type's forms, Elation deletes all of that type's rows and writes new ones. Two consequences follow:

* `id` is not stable. The same form on the same appointment type gets a new `id` on every save, so `id` cannot be used to track a form over time. Use `appt_type_id` with `patient_form_id` instead.
* A form that is still on the type will also have older rows carrying a `deletion_time`. Counting rows overstates how many forms a type asks for, so count distinct `patient_form_id` among the rows where `deletion_time is null`.

This also means `deletion_time` on its own does not say a form was removed. It says the row stopped applying, which is usually just a resave. A form was genuinely removed only when it has no row with `deletion_time is null`, as the [forms removed from an appointment type](#forms-removed-from-an-appointment-type) query shows.

### Only today's setup is kept

There is no record of when a current form was added to a type. A query that works out the forms a past appointment required will give you the type's setup as it stands today, not as it stood on the day of the appointment. Treat "forms required" as a current-state answer, not a historical one.

### The forms an appointment requires, matching Elation

Join `appointment.appt_type_id` to `appt_type_id` here, keeping rows where `deletion_time is null`. Left join `patient_form` on `patient_form_id` and leave out forms whose `patient_form.deletion_time` is set; the left join matters because a few older forms are not in your Hosted Database at all. Then apply the per-appointment overrides: remove forms with a `not_required` change for that appointment, and add forms with a `required` change. See [Per-appointment form changes](https://help.elationhealth.com/articles/hdb/patient-form-appointment-change.md).

### Appointment types have no table of their own

There is no `appt_type` table. An appointment type's name lives on the appointments that use it, as `appointment.appt_type`. Pick the name up from there when you need to label a type.

### Two forms can share a `sequence`

Where they do, Elation lists an appointment's required forms in `patient_form.sequence` order instead. Sort by both to match what the practice sees.

## An appointment type's current forms

Returns the forms one appointment type currently asks for, in the order Elation lists them.

```sql sql theme={null}
select
    t.appt_type_id
  , t.practice_id
  , t.sequence as position_on_type
  , t.patient_form_id
  , f.name as form_name
  , f.title as form_title
  , f.sequence as position_in_form_list
from patient_form_appointment_type t
  left join patient_form f on f.id = t.patient_form_id
where t.appt_type_id = <appt_type_id>
  and t.deletion_time is null
  and (f.id is null or f.deletion_time is null)
order by t.sequence, f.sequence, t.patient_form_id;
```

## Forms removed from an appointment type

Finds forms that an appointment type used to ask for and no longer does, with the time they stopped applying. A form counts as removed only when it has no current row on the type.

```sql sql theme={null}
select
    t.appt_type_id
  , t.practice_id
  , t.patient_form_id
  , f.title as form_title
  , max(t.deletion_time) as removed_at
from patient_form_appointment_type t
  left join patient_form f on f.id = t.patient_form_id
where t.deletion_time is not null
  and not exists (
      select 1
      from patient_form_appointment_type current_row
      where current_row.appt_type_id = t.appt_type_id
        and current_row.patient_form_id = t.patient_form_id
        and current_row.deletion_time is null
  )
group by t.appt_type_id, t.practice_id, t.patient_form_id, f.title
order by removed_at desc
limit 100;
```

## The forms one appointment requires

Applies the full recipe to a single appointment: the type's current forms, minus any removed for this appointment, plus any added for it.

```sql sql theme={null}
with appt as (
    select a.id, a.practice_id, a.appt_type_id, a.appt_type, a.appt_time
    from appointment a
    where a.id = <appointment_id>
)
select
    appt.id as appointment_id
  , appt.appt_type
  , appt.appt_time
  , required.patient_form_id
  , f.title as form_title
  , required.source
from appt
  join (
      select appt.id as appointment_id, t.patient_form_id, 'appointment type' as source
      from appt
        join patient_form_appointment_type t
          on t.appt_type_id = appt.appt_type_id
         and t.deletion_time is null
      where not exists (
          select 1
          from patient_form_appointment_change c
          where c.appointment_id = appt.id
            and c.patient_form_id = t.patient_form_id
            and c.override_type = 'not_required'
            and c.deletion_time is null
      )
      union
      select appt.id, c.patient_form_id, 'added for this appointment'
      from appt
        join patient_form_appointment_change c
          on c.appointment_id = appt.id
         and c.override_type = 'required'
         and c.deletion_time is null
  ) required on required.appointment_id = appt.id
  left join patient_form f on f.id = required.patient_form_id
where f.id is null or f.deletion_time is null
order by f.sequence, required.patient_form_id;
```

*If you have any questions about this topic please reach out to [Elation Support Portal](https://help.elationhealth.com/articles/support-portal-introduction.md) with the subject line HDB - \<your\_question>*
