# Input Var

<p>An <code>input_var</code> adds a calculated column to an <a href="https://www.rudderstack.com/docs/profiles/concepts/inputs/" >input</a> table. It calculates a single value per row of the input table which can be used as an input for <a href="https://www.rudderstack.com/docs/profiles/dev-docs/profiles-yaml/var-groups/entity-var/" ><code>entity_vars</code></a>.</p>

<blockquote class="warning">
  <div class="tip-quote">
    
    <div class="tip-text"><p><strong>Performance consideration</strong>: Input vars scan all input data on every run, which can be expensive for large datasets.</p>
<p>Consider using the following approaches for better performance:</p>
<ul>
<li><a href="https://www.rudderstack.com/docs/profiles/dev-docs/incremental-features/make-features-incremental/" ><code>entity_var</code> with Incremental Features</a> for aggregations like <code>SUM</code>, <code>COUNT</code>, <code>MIN</code>, <code>MAX</code></li>
<li><a href="https://www.rudderstack.com/docs/profiles/dev-docs/incremental-features/incremental-sql-models/" >Incremental SQL Models</a> for complex transformations</li>
</ul>
<p>Use <code>input_var</code> only when you need row-level calculations on the input table before entity-level aggregation (for example, window functions like <code>ROW_NUMBER()</code> or <code>LAG()</code>).</p>
</div>
  </div>
</blockquote>
<div class="rs-code">
  <div class="rs-code__head">yaml<button class="rs-code__copy" type="button">
      
      Copy
    </button>
  </div>
  <div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml">- input_var:
    name: input_var_name
    select: SQL statement
    from: inputs/table
    window:
        partition_by:
            - partition_column
    description: description</code></pre></div>
</div>
<table>
  <thead>
      <tr>
          <th>Field</th>
          <th>Description</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>name</code></td>
          <td>Name of the <code>input_var</code>.</td>
      </tr>
      <tr>
          <td><code>select</code></td>
          <td>SQL state, equivalent to <code>SELECT</code> statement.</td>
      </tr>
      <tr>
          <td><code>from</code></td>
          <td>Path of the input table, equivalent to <code>FROM</code> statement.</td>
      </tr>
      <tr>
          <td><code>where</code></td>
          <td>Filtering condition, equivalent to <code>WHERE</code> statement.</td>
      </tr>
      <tr>
          <td><a href="https://www.rudderstack.com/docs/profiles/dev-docs/profiles-yaml/var-groups/window-functions/" ><code>window</code></a></td>
          <td>Adds <code>partition_by</code> and/or <code>order_by</code> keys for window functions</td>
      </tr>
      <tr>
          <td><code>description</code></td>
          <td>Description of the <code>input_var</code>.</td>
      </tr>
  </tbody>
</table>
<!-- end-chunk -->
<!-- begin-chunk -->
<h2 id="example">Example</h2><div class="rs-code">
  <div class="rs-code__head">yaml<button class="rs-code__copy" type="button">
      
      Copy
    </button>
  </div>
  <div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml">- input_var:
    name: page_count
    description: Adds column to a SQL model to get a page count per date.
    select: count(distinct url)
    from: models/pages_orderby_table
    window:
        partition_by:
            - profile_id
            - date</code></pre></div>
</div>
<!-- end-chunk -->
<!-- begin-chunk -->
<h2 id="migrating-input-vars">Migrate input vars to incremental alternatives</h2><p>Input vars scan all input data on every run and <strong>block incremental processing</strong> for any entity var that depends on them. When migrating a project to incremental mode, you must convert or replace every input var.</p>
<!-- end-chunk -->
<!-- begin-chunk -->
<h3 id="decision-tree">Decision tree</h3><p>For each input var, follow this decision tree to choose the best replacement:</p>
<div class="rs-code">
  <div class="rs-code__head"><button class="rs-code__copy" type="button">
      
      Copy
    </button>
  </div>
  <div class="highlight"><pre class="chroma"><code class="language-text" data-lang="text">Input Var
+-- Uses LAST_VALUE / FIRST_VALUE window function?
|   +-- Yes: Convert to entity_var using MAX_BY / MIN_BY
|
+-- Uses a simple aggregation with partition_by?
|   +-- Yes: Convert to entity_var with merge clause
|
+-- Requires complex multi-step logic?
    +-- Yes: Create an incremental SQL model and
        reference it from entity vars</code></pre></div>
</div>
<!-- end-chunk -->
<!-- begin-chunk -->
<h3 id="option-1-convert-to-entity-var-most-common">Option 1: Convert to entity var (most common)</h3><p>You can replace window functions like <code>LAST_VALUE</code> and <code>FIRST_VALUE</code> with <code>MAX_BY</code> and <code>MIN_BY</code>:</p>
<div class="rs-code">
  <div class="rs-code__head">yaml<button class="rs-code__copy" type="button">
      
      Copy
    </button>
  </div>
  <div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml"># Before (input var with LAST_VALUE)
- input_var:
    name: last_known_city
    select: last_value(city)
    from: inputs/events
    window:
      partition_by:
        - user_id
      order_by:
        - timestamp
    where: city IS NOT NULL

# After (entity var with MAX_BY + ordering helper)
- entity_var:
    name: last_known_city
    select: MAX_BY({{events.city}}, {{events.timestamp}})
    merge: MAX_BY({{rowset.last_known_city}}, {{rowset.last_known_city_by_param}})
    from: inputs/events
    where: {{events.city}} IS NOT NULL

- entity_var:
    name: last_known_city_by_param
    select: MAX({{events.timestamp}})
    merge: MAX({{rowset.last_known_city_by_param}})
    from: inputs/events
    where: {{events.city}} IS NOT NULL
    is_feature: false</code></pre></div>
</div>
<p>For detailed patterns, see the following sections:</p>
<ul>
<li><a href="https://www.rudderstack.com/docs/profiles/dev-docs/incremental-features/compound-aggregations/#min_by--max_by-with-ordering-helper" >MIN_BY/MAX_BY with ordering helper</a></li>
<li><a href="https://www.rudderstack.com/docs/profiles/dev-docs/incremental-features/compound-aggregations/#replace-window-functions" >Replace window functions</a></li>
</ul>
<!-- end-chunk -->
<!-- begin-chunk -->
<h3 id="option-2-create-an-incremental-sql-model">Option 2: Create an incremental SQL model</h3><p>When the input var logic is too complex for an entity var (multi-step transformations, complex joins, or logic that truly requires row-level windowing), create an <a href="https://www.rudderstack.com/docs/profiles/dev-docs/incremental-features/incremental-sql-models/" >Incremental SQL Model</a> that handles the computation incrementally, then reference it from entity vars.</p>
<br />
