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

# Query on a previous result

> Build complex analyses by querying the results of completed query runs.

export const CopyIcon = props => <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" className="lucide lucide-copy-icon lucide-copy inline-block" {...props}>
    <rect width="14" height="14" x="8" y="8" rx="2" ry="2" />
    <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
  </svg>;

You can reference the results of any completed query run in a new query. This enables you to build multi-stage analyses, perform additional aggregations, or join datasets without re-executing expensive queries.

## How It Works

<Steps>
  <Step title="Run Your Initial Query">
    Execute any query in the Allium App. Once complete, the results are stored and can be referenced by other queries.
  </Step>

  <Step title="Access Run History">
    On the query editor page, click **View Run History** to see all completed runs:

    <Frame>
      <img src="https://mintcdn.com/allium-e770e2b7/Iw3K4hWzAMJB-mUx/images/run-query-run-history.png?fit=max&auto=format&n=Iw3K4hWzAMJB-mUx&q=85&s=3ce8c816167653ec6e4b6a3b69bd716a" alt="Query run history" width="2124" height="1312" data-path="images/run-query-run-history.png" />
    </Frame>
  </Step>

  <Step title="Copy the Run ID">
    Find the query run you want to reference and click the copy button <CopyIcon width={14} height={14} /> to copy its Run ID.
  </Step>

  <Step title="Reference in a New Query">
    Use the Run ID in a new query with the format `query_history.<query_run_id>`:

    ```sql theme={null}
    select sum(usd_volume)
    from query_history.hoHTRjrs2MO3X64I5Rc2_20251001T080527_horde;
    ```

    You can perform any SQL operations on the query history results, including joins, aggregations, and filtering.
  </Step>
</Steps>

## Time Limits

<Note>
  **Query History Time Constraints**

  * **Mixed queries**: When combining `query_history` results with Allium data tables, query results must be less than 24 hours old
  * **Query history only**: When querying exclusively from `query_history` tables, there is no time limit
</Note>

## Use Cases

Querying previous results is useful for:

* Breaking down complex analyses into manageable steps
* Joining pre-computed datasets without re-running expensive queries
* Performing multiple aggregations on the same base dataset
* Creating derived tables for further analysis
* Iterating on analysis without recomputing source data

## Example: Multi-Stage Analysis

```sql theme={null}
-- Step 1: Run a query to get daily DEX volumes (save as query_id_1)
select
  date(block_timestamp) as date,
  chain,
  sum(usd_amount) as daily_volume
from crosschain.dex.trades
where block_timestamp >= current_timestamp - interval '30 days'
group by all;

-- Step 2: Calculate 7-day moving average from the first query
select
  date,
  chain,
  daily_volume,
  avg(daily_volume) over (
    partition by chain 
    order by date 
    rows between 6 preceding and current row
  ) as ma_7d
from query_history.query_id_1_20251015T120000_horde
order by chain, date;
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Basic Queries" href="/app/run-queries/basic-query">
    Learn the fundamentals of running queries
  </Card>

  <Card title="Parameterized Queries" href="/app/run-queries/parameterized-query">
    Create reusable queries with configurable parameters
  </Card>

  <Card title="Explorer API" href="/api/explorer/overview">
    Access query results programmatically
  </Card>

  <Card title="Compute Profiles" href="/app/query-management/compute-profiles">
    Optimize performance for complex queries
  </Card>
</CardGroup>
