# Named Query Working Examples

Use these examples when you already understand the basic named query structure and want patterns you can reuse.

### Example 1: Display an aggregate value

Use an aggregate named query when you need a count or sum in plain text.

{% code title="aggregate-named-query.xml" %}

```xml
<!--{{!
<lineitemsSOQL>
  <class>none</class>
  <queryname>LineItemStats</queryname>
  <soql>
    SELECT COUNT(Id) cid, SUM(TotalPrice) asum
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!Opportunity.Id}}'
  </soql>
</lineitemsSOQL>
}}-->
```

{% endcode %}

{% code title="aggregate-output.txt" %}

```plaintext
This opportunity has {{!LineItemStats.cid}} line items totaling ${{!LineItemStats.asum #,###.00}}.
```

{% endcode %}

Use aliases like `cid` and `asum` in the `SELECT` clause.

Then reference those aliases in the merge field.

### Example 2: Use `offset` to pull specific rows

Use `offset` when your query returns multiple rows and you want one specific result.

{% code title="offset-named-query.xml" %}

```xml
<!--{{!
<lineitemsSOQL>
  <class>none</class>
  <queryname>SortedItems</queryname>
  <soql>
    SELECT Name, TotalPrice
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!Opportunity.Id}}'
    ORDER BY TotalPrice DESC
  </soql>
</lineitemsSOQL>
}}-->
```

{% endcode %}

{% code title="offset-output.txt" %}

```plaintext
Highest-value item: {{!SortedItems.Name offset="1"}}
Second highest-value item: {{!SortedItems.Name offset="2"}}
```

{% endcode %}

`offset="1"` returns the first row.

`offset="2"` returns the second row.

### Example 3: Use `filter` with `offset`

Use `filter` when you want reusable conditions inside one named query.

{% code title="filter-named-query.xml" %}

```xml
<!--{{!
<lineitemsSOQL>
  <class>none</class>
  <queryname>FilteredItems</queryname>
  <soql>
    SELECT Name, TotalPrice
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!Opportunity.Id}}'
    ORDER BY TotalPrice DESC
  </soql>
  <filter id="1">TotalPrice >= 1000</filter>
</lineitemsSOQL>
}}-->
```

{% endcode %}

{% code title="filter-output.txt" %}

```plaintext
Highest line item over $1,000: {{!FilteredItems.Name filter="1" offset="1"}}
Second line item over $1,000: {{!FilteredItems.Name filter="1" offset="2"}}
```

{% endcode %}

If your sample data uses smaller values, lower the filter threshold.

{% hint style="info" %}
For DOCX and PPTX templates, wrap both the query block and merge fields in square brackets.
{% endhint %}

### Keep going

* Review [Reference: Named Query Syntax](/developer-hub/template-configuration-and-styling/reference-named-query-syntax.md) for the full tag and attribute reference
* Review [How to: Advanced Data Retrieval with Named Queries](https://github.com/kvantiem-sdocs/Developer-Hub-Documentation/blob/main/advanced-template-logic/named-query/how-to-advanced-data-retrieval-with-named-queries.md) for more advanced patterns


---

# 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://help.sdocs.com/developer-hub/template-configuration-and-styling/reference-named-query-syntax/named-query-working-examples.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.
