AI Aggregate Function

The AI_AGG function performs AI evaluation at the aggregation level rather than per row. This function concatenates input text from multiple rows into a single aggregated string and sends that text to the model once per group.

Note:

For information about limitations with using AI Functions, see Known Issues with AI Functions.

If no GROUP BY clause is present, all rows are treated as a single group.

If grouping is present, one AI result is produced per group.

Use this function for:

  • Group-level summaries
  • Trend or theme extraction
  • Aggregated sentiment analysis
  • Rollup insights

Syntax

AI_AGG(<input>, <prompt>, <model_id>)

Parameters

Parameter Description
input

String

  • Row-level text that is aggregated by concatenation.
  • Must be a string or expression explicitly cast to string.
prompt

String

  • Instructions describing what the model should generate for the group.
  • Must be a string literal or expression.
model_id

String literal

  • Identifier of the model asset to use.

Examples

This example summarizes all reviews.

SELECT
	AI_AGG(
		'Reviews: ' || REVIEW_TEXT,
		'Summarize all the reviews in one paragraph. No more than 4 sentences. Return only the summary.',
		'model'.'id'
	)
FROM REVIEWS;

This example summarizing all reviews per product.

SELECT 
          PRODUCT_ID,  AI_AGG(  'Reviews: ' ||
          REVIEW_TEXT,  'Summarize all the reviews per product in one
          paragraph. No more than 4 sentences. Return only the summary.',  'model'.'id'  ) FROM REVIEWS GROUP BY PRODUCT_ID;

Best Practices

Keep these best practices in mind when you use the AI_AGG function:

  • Avoid vague prompts. Be sure to clearly describe what the model should generate for the group.
  • Specify the desired output format explicitly to prevent extra or unexpected text.
  • Provide descriptive labels for each input value to help the model understand what each piece of data represents.
  • Ensure the prompt references the same labels used in the input.
  • Use punctuation and separators to clearly distinguish between multiple input values.
  • Cast non-string expressions and columns to string.
  • Use consistent and predictable labels across queries to improve reliability.
  • Keep the number of rows per group reasonable.
  • Keep the number of groups reasonable.