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

# SELECT clause

> Use the SELECT clause in Junction Sense queries to choose table columns, apply aggregate functions, and retrieve group key columns.

## Select a table column

Select a specific table column using a [Table Column expression](/sense/query-dsl/column-expressions).

<Accordion title="Output column name" icon="line-columns" iconType="duotone" defaultOpen>
  The name of the column being selected.
</Accordion>

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(va.Sleep.col("efficiency"))
  ```

  ```jsonc JSON DSL theme={null}
  {
      "select": [
          { "sleep": "efficiency" }
      ]
  }
  ```
</CodeGroup>

## Perform an analysis and select the output value

Perform data analysis with in-built algorithms using value macro expressions:

* [Sleep Analysis](/sense/query-dsl/sleep-analysis) value macro expressions

<Accordion title="Output column name" icon="line-columns" iconType="duotone" defaultOpen>
  The name of the value macro, e.g., `chronotype` for the Chronotype value macro.
</Accordion>

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(va.Sleep.chronotype())
  ```

  ```jsonc JSON DSL theme={null}
  {
      "select": [
          { "value_macro": "chronotype" }
      ]
  }
  ```
</CodeGroup>

## Aggregate a table column

Aggregate a [Table Column expression](/sense/query-dsl/column-expressions) with respect to the `group_by` clause.

The specified aggregate function is applied to each and every group created by the `group_by` clause.
The result set is N aggregated values where N is the number of groups.

<AccordionGroup>
  <Accordion title="Statistical functions" icon="chart-column" iconType="duotone" defaultOpen>
    | Aggregate function | Python DSL       | JSON DSL                             |
    | ------------------ | ---------------- | ------------------------------------ |
    | Minimum            | `$EXPR.min()`    | `{ "func": "min", "arg": $EXPR }`    |
    | Maximum            | `$EXPR.max()`    | `{ "func": "max", "arg": $EXPR }`    |
    | Mean               | `$EXPR.mean()`   | `{ "func": "mean", "arg": $EXPR }`   |
    | Median             | `$EXPR.median()` | `{ "func": "median", "arg": $EXPR }` |
    | Standard Deviation | `$EXPR.stddev()` | `{ "func": "stddev", "arg": $EXPR }` |
    | Count              | `$EXPR.count()`  | `{ "func": "count", "arg": $EXPR }`  |
    | Oldest Value       | `$EXPR.oldest()` | `{ "func": "oldest", "arg": $EXPR }` |
    | Newest Value       | `$EXPR.newest()` | `{ "func": "newest", "arg": $EXPR }` |
  </Accordion>

  <Accordion title="Math functions" icon="sigma" iconType="duotone" defaultOpen>
    | Aggregate function | Python DSL    | JSON DSL                          |
    | ------------------ | ------------- | --------------------------------- |
    | Sum                | `$EXPR.sum()` | `{ "func": "sum", "arg": $EXPR }` |
  </Accordion>

  <Accordion title="Output column name" icon="line-columns" iconType="duotone" defaultOpen>
    * the aggregation function name, e.g., `sum`; or
    * if the Split by Source mode is enabled — `$FUNCTION_NAME.$SOURCE_COLUMN_NAME`, e.g., `mean.efficiency`.
  </Accordion>
</AccordionGroup>

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.Sleep.col("stage_asleep_second").newest()
  )
  ```

  ```jsonc JSON DSL theme={null}
  {
      "select": [
          {
              "func": "newest",
              "arg": { "sleep": "stage_asleep_second" }
          }
      ]
  }
  ```
</CodeGroup>

## Aggregate list-column elements

Resources like Menstrual Cycle expose list-of-struct columns (for example, `basal_body_temperature` is a list of `{date, value}` entries). Aggregate their elements with a scalar-output subquery — a self-contained `select` / `from` (UNNEST) / optional `where` block that produces one scalar per outer row.

<Accordion title="Output column name" icon="line-columns" iconType="duotone" defaultOpen>
  * `$FUNC.$COLUMN` when the subquery aggregates rows directly (e.g., `count.menstrual_flow`); or
  * `$FUNC.$COLUMN.$FIELD` when a struct field is extracted (e.g., `mean.basal_body_temperature.value`); or
  * `$FUNC.$COLUMN.element` when the scalar-list element itself is aggregated (e.g., `sum.tags.element`).
</Accordion>

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(
      va.MenstrualCycle.col("menstrual_flow")
          .unnest_and_select(lambda col: col.count())
  )
  ```

  ```jsonc JSON DSL theme={null}
  {
      "select": [
          {
              "select": { "func": "count", "arg": null },
              "from": { "unnest": { "menstrual_cycle": "menstrual_flow" } }
          }
      ]
  }
  ```
</CodeGroup>

See [List-column aggregation](/sense/query-dsl/list-column-aggregation) for the full reference, including the `where` predicate, struct-field extraction, and the reserved `element` identifier for scalar-element lists.

## Select the Index Column

Select the primary datetime index of the table using the [Index Column expression](/sense/query-dsl/column-expressions#index-column-expression).

<Accordion title="Output column name" icon="line-columns" iconType="duotone" defaultOpen>
  `timestamp` (constant).
</Accordion>

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  va.select(va.Sleep.index())
  ```

  ```jsonc JSON DSL theme={null}
  {
      "select": [
          { "index": "sleep" }
      ]
  }
  ```
</CodeGroup>

## Select the Group Key Columns

Select the [Group Key Columns](/sense/query-dsl/column-expressions#group-key-column-expression) associated
with the `group_by` clause.

You can select one specific group key column by offset, or select all group key columns with a `*` wildcard.

<Accordion title="Output column name" icon="line-columns" iconType="duotone" defaultOpen>
  `group_key.$OFFSET`, where `$OFFSET` corresponds to the N-th expression of the `group_by` clause.
</Accordion>

<CodeGroup>
  ```python Python DSL theme={null}
  import vitalx.aggregation as va

  # Select all group key columns
  va.select(va.group_key("*"))

  # Select the 2nd group key column
  va.select(va.group_key(2))
  ```

  ```jsonc JSON DSL theme={null}
  # Select all group key columns
  {
      "select": [
          { "group_key": "*" }
      ]
  }

  # Select the 2nd group key column
  {
      "select": [
          { "group_key": 2 }
      ]
  }
  ```
</CodeGroup>
