> For the complete documentation index, see [llms.txt](https://upsolver.gitbook.io/content/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://upsolver.gitbook.io/content/quickstarts-1/jobs/transformation/updating-data/aggregate-and-output-data.md).

# Aggregate and Output Data

Using familiar SQL syntax in your transformation job, you can aggregate the data in your staging table and load it into your target table.&#x20;

**Here's** **the code to create a job that loads aggregated data into your target table:**

```sql
CREATE JOB group_testing
    START_FROM = BEGINNING
    RUN_INTERVAL = 1 MINUTE
    ADD_MISSING_COLUMNS = TRUE
AS INSERT INTO default_glue_catalog.upsolver_samples.orders_aggregated_data 
    MAP_COLUMNS_BY_NAME
       SELECT customer.address.city AS customer_address_city, 
          SUM(nettotal) AS total_revenue,
          COUNT(DISTINCT orderid) AS num_orders
       FROM default_glue_catalog.upsolver_samples.orders_raw_data
       WHERE $commit_time BETWEEN run_start_time() AND run_end_time()
       GROUP BY customer.address.city;
```

Let's understand what this code does.

In this example, you create a job named **group\_testing**, and set the target table for your data as **orders\_aggregated\_data**. The job option `START_FROM` instructs Upsolver to include all historical data, and the `RUN_INTERVAL` option specifies that the job should execute every **1 minute**. The option `ADD_MISSING_COLUMNS` ensures that all columns that are added to the source table in the future, are also added to the target table.&#x20;

In the `SELECT` statement, we are aggregating our sample orders by **customer.address.city**, calculating the total revenue per city. Let's break it down:

* **SUM(nettotal) AS total\_revenue**: in this line, we sum the **nettotal** column and rename it to **total\_revenue**. This provides us with the total revenue by city.
* **COUNT(DISTINCT orderid) AS num\_orders**: we then count each the number of orders by city into a new column named **num\_orders**.&#x20;
* **GROUP BY customer.address.city**: this line groups our previous columns by **customer.address.city**, enabling us to view total revenue and the number of orders by city.

When the job is running and we have data in our target table, the final step is to query the data.

**Here's** **the code for querying your data:**

```sql
SELECT * 
FROM default_glue_catalog.upsolver_samples.orders_aggregated_data 
LIMIT 10;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://upsolver.gitbook.io/content/quickstarts-1/jobs/transformation/updating-data/aggregate-and-output-data.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
