# Select first/last encounter

> E.g.: I want the date of the first and last hospital admission for every patient in my table

Let's take an example table which contains all hospital visits in one facility over many years. Each record contains information about what happened in the visit, including a patient id to identify who was present. An individual patient might be represented in one or many records in this table since each record represents a separate hospital visit.&#x20;

| patient\_id | encounter\_date |
| ----------- | --------------- |
| 1           | 2012-01-01      |
| 1           | 2011-01-01      |
| 1           | 2010-01-01      |
| 1           | 2013-01-01      |
| 2           | 2009-01-01      |
| 2           | 2008-01-01      |
| 2           | 2015-01-01      |
| 3           | 2014-02-01      |

Let's say that for our research we want to find the first and last encounter — or more formally, the min and max encounter date for each patient.

Computing a min or max value is an aggregate operation that scans all records and chooses the highest or lowest value. To compute the min or max value in a variable we will want to use an analytic [new variable](/reference/workflows/transforms/step-create-variables.md) method. We can do this either by [aggregating](/reference/workflows/transforms/step-aggregate.md) our table (which would drop records and/or variables) or by using a partition to calculate a new variable without changing the rest of the data.&#x20;

In this example we will use a partition to create a new variable since we want the rest of the data to remain unchanged.&#x20;

## Variable concepts

Conceptually we will define our **aggregation** variable as the date of the encounter since that is where we want to look for the min and max values.

We will define our **partition** as the patient id since we want to find the min or max for each patient. This will limit the min or max value scan to all values of our aggregation variable across each unique value of our partitioned variable (patient id).&#x20;

You can see more examples of using partitions with analytic methods on the [Create variables](/reference/workflows/transforms/step-create-variables.md) step page.

## Defining the new variable

For this particular case, we want to find the **MIN** and **MAX** of the `encounter_date` of each patient; in other words, partitioned on `patient_id` . This will create two new variables, `min_encounter_date` and `max_encounter_date` for each patient.&#x20;

<figure><img src="/files/wc0Wew4LPFgw5JqxZ2i6" alt=""><figcaption></figcaption></figure>

When we run this query, note that no records will be dropped; rather, these new variables will be have consistent values for any given `patient_id` .&#x20;

| patient\_id | encounter\_date | min\_encounter\_date | max\_encounter\_date |
| ----------- | --------------- | -------------------- | -------------------- |
| 1           | 2012-01-01      | 2010-01-01           | 2013-01-01           |
| 1           | 2011-01-01      | 2010-01-01           | 2013-01-01           |
| 1           | 2010-01-01      | 2010-01-01           | 2013-01-01           |
| 1           | 2013-01-01      | 2010-01-01           | 2013-01-01           |
| 2           | 2009-01-01      | 2008-01-01           | 2015-01-01           |
| 2           | 2008-01-01      | 2008-01-01           | 2015-01-01           |
| 2           | 2015-01-01      | 2008-01-01           | 2015-01-01           |
| 3           | 2014-02-01      | 2014-02-01           | 2014-02-01           |

Now, let's say we only want to keep records which contain each patient's first and last encounter. We can do this easily be creating a new row filter:

<figure><img src="/files/KrcossNDcDlKJSyMwB5R" alt=""><figcaption></figcaption></figure>

We can also generalize this approach to find the Nth encounter for each patient — take a look at the **RANK**, **DENSE\_RANK**, and **ROW\_NUMBER** methods.&#x20;

And if we want to be more specific on our partition (e.g., for the first / last encounter in a given calendar year), we can always apply additional variables (`year` ) when defining our partition.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.redivis.com/guides/analyze-data-in-a-workflow/example-workflows/select-first-last-encounter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
